|
| 1 | +--- |
| 2 | +Title: 'reduceRight()' |
| 3 | +Description: 'Applies a reducer function to array elements from right to left, accumulating a single output value.' |
| 4 | +Subjects: |
| 5 | + - 'Computer Science' |
| 6 | + - 'Web Development' |
| 7 | +Tags: |
| 8 | + - 'Arrays' |
| 9 | + - 'JavaScript' |
| 10 | + - 'Methods' |
| 11 | +CatalogContent: |
| 12 | + - 'introduction-to-javascript' |
| 13 | + - 'paths/front-end-engineer-career-path' |
| 14 | +--- |
| 15 | + |
| 16 | +The **`reduceRight()`** method in JavaScript executes a reducer function on each element of an array, from right to left, to produce a single accumulated result. It is commonly used when the order of operations matters—such as evaluating expressions, parsing nested structures, or performing right-associative computations like exponentiation. |
| 17 | + |
| 18 | +## Syntax |
| 19 | + |
| 20 | +```pseudo |
| 21 | +reduceRight(callbackFn, initialValue) |
| 22 | +``` |
| 23 | + |
| 24 | +**Parameters:** |
| 25 | + |
| 26 | +- `callbackFn`: A function to execute on each element in the array. It takes four arguments: |
| 27 | + - `accumulator`: The accumulated result from the previous callback. |
| 28 | + - `currentValue`: The current element being processed. |
| 29 | + - `currentIndex`: The index of the current element. |
| 30 | + - `array`: The array `reduceRight()` was called upon. |
| 31 | +- `initialValue` (Optional): A value to use as the first argument to the first call of `callbackFn`. If not provided, the last element in the array is used as the initial value, and iteration starts from the second-to-last element. |
| 32 | + |
| 33 | +**Return value:** |
| 34 | + |
| 35 | +Returns a single value resulting from the reduction of the array, working from right to left. |
| 36 | + |
| 37 | +## `reduceRight()` vs `reduce()` |
| 38 | + |
| 39 | +- [`reduce()`](https://www.codecademy.com/resources/docs/javascript/arrays/reduce) processes array elements from left to right, useful for left-associative operations like summing or accumulating values. |
| 40 | +- `reduceRight()` processes elements from right to left, ideal for right-associative logic like parsing or reversing operations. |
| 41 | + |
| 42 | +In the following example, `reduce()` combines array elements from left to right, while `reduceRight()` combines them from right to left, resulting in reversed concatenation: |
| 43 | + |
| 44 | +```js |
| 45 | +const stringArray = ['0', '2', '4', '6', '🐈', '→']; |
| 46 | + |
| 47 | +const reduceMethod = stringArray.reduce((prev, cur) => prev + cur); |
| 48 | +const reduceRightMethod = stringArray.reduceRight((prev, cur) => prev + cur); |
| 49 | + |
| 50 | +console.log(`reduceMethod : ${reduceMethod}`); |
| 51 | +console.log(`reduceRightMethod : ${reduceRightMethod}`); |
| 52 | +``` |
| 53 | + |
| 54 | +It generates the following output: |
| 55 | + |
| 56 | +```shell |
| 57 | +reduceMethod : 0246🐈→ |
| 58 | +reduceRightMethod : →🐈6420 |
| 59 | +``` |
| 60 | + |
| 61 | +## Example 1: Reverse Sum with `reduceRight()` |
| 62 | + |
| 63 | +The following code shows the `reduceRight()` without an initial value, and the order of execution of the callback: |
| 64 | + |
| 65 | +```js |
| 66 | +const michiArray = [0, 1, 2, 3, 4]; |
| 67 | +const reducedMichi = michiArray.reduceRight( |
| 68 | + (accumulator, currentValue) => accumulator + currentValue |
| 69 | +); |
| 70 | + |
| 71 | +console.log(reducedMichi); |
| 72 | +``` |
| 73 | + |
| 74 | +| call # | accumulator | currentValue | index | Return value | |
| 75 | +| ----------- | ----------- | ------------ | ----- | ------------ | |
| 76 | +| First call | 4 | 3 | 3 | 7 | |
| 77 | +| Second call | 7 | 2 | 2 | 9 | |
| 78 | +| Third call | 9 | 1 | 1 | 10 | |
| 79 | +| Fourth call | 10 | 0 | 0 | 10 | |
| 80 | + |
| 81 | +The output of this code is: |
| 82 | + |
| 83 | +```shell |
| 84 | +10 |
| 85 | +``` |
| 86 | + |
| 87 | +## Example 2: Reverse Name Construction with `reduceRight()` |
| 88 | + |
| 89 | +The following code shows the `reduceRight()` with an initial value, and the order of execution of the callback: |
| 90 | + |
| 91 | +```js |
| 92 | +const favoriteName = ['👑', 'y', 'n', 'a', 'i']; |
| 93 | +const princess = favoriteName.reduceRight( |
| 94 | + (accumulator, currentValue) => accumulator + currentValue, |
| 95 | + 'L' |
| 96 | +); |
| 97 | + |
| 98 | +console.log(princess); |
| 99 | +``` |
| 100 | + |
| 101 | +| call # | accumulator | currentValue | index | Return value | |
| 102 | +| ----------- | ----------- | ------------ | ----- | ------------ | |
| 103 | +| First call | L | i | 4 | Li | |
| 104 | +| Second call | Li | a | 3 | Lia | |
| 105 | +| Third call | Lia | n | 2 | Lian | |
| 106 | +| Fourth call | Lian | y | 1 | Liany | |
| 107 | +| Fifth call | Liany | 👑 | 0 | Liany👑 | |
| 108 | + |
| 109 | +The output of this code is: |
| 110 | + |
| 111 | +```shell |
| 112 | +Liany👑 |
| 113 | +``` |
| 114 | + |
| 115 | +## Codebyte Example |
| 116 | + |
| 117 | +Run the following code to understand the working of the `reduceRight()` method: |
| 118 | + |
| 119 | +```codebyte/javascript |
| 120 | +const codingMessage = ["practice", " ", "of", " ", "lot", " ", "a", " ", "require", ", ", "code", " ", "to", " "]; |
| 121 | +
|
| 122 | +const fullMessage = codingMessage.reduceRight( |
| 123 | + (accumulator, currentValue) => accumulator + currentValue, "learning" |
| 124 | +); |
| 125 | +
|
| 126 | +console.log(fullMessage); |
| 127 | +``` |
0 commit comments