Skip to content

WebDevHubs

  • Home
  • JavaScript
  • Toggle search form

JavaScript Array reduceRight() Method

Posted on December 31, 2024December 31, 2024 By Admin No Comments on JavaScript Array reduceRight() Method

The Array.reduceRight() method is a built-in JavaScript function that processes each element of an array from right to left (starting from the last element to the first) and reduces it to a single output value.

Syntax

arr.reduceRight(callback(accumulator, currentValue, index, array), initialValue);

Parameters

ParameterDescription
callback(Required) A function to execute on each element in the array.
accumulator(Required in callback) The accumulated result from previous callback executions.
currentValue(Required in callback) The current element being processed.
index(Optional in callback) The index of the current element being processed.
array(Optional in callback) The array reduceRight() is being applied on.
initialValue(Optional) A value to use as the initial accumulator. If not provided, the last element of the array is used.

Return Value

The reduceRight() method returns the final accumulated value after iterating through all elements of the array from right to left.

Example 1: Concatenating Strings in Reverse Order

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let words = ['world', ' ', 'hello'];
let result = words.reduceRight((accumulator, currentValue) => accumulator + currentValue, '');
console.log(result); // Output: 'hello world'
let words = ['world', ' ', 'hello']; let result = words.reduceRight((accumulator, currentValue) => accumulator + currentValue, ''); console.log(result); // Output: 'hello world'
let words = ['world', ' ', 'hello'];

let result = words.reduceRight((accumulator, currentValue) => accumulator + currentValue, '');
console.log(result); // Output: 'hello world'

Example 2: Summing Array Elements

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [1, 2, 3, 4];
let sum = arr.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 10
let arr = [1, 2, 3, 4]; let sum = arr.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0); console.log(sum); // Output: 10
let arr = [1, 2, 3, 4];
let sum = arr.reduceRight((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // Output: 10

Example 3: Flattening a Nested Array in Reverse Order

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let arr = [[5, 6], [3, 4], [1, 2]];
let flatArray = arr.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue), []);
console.log(flatArray);
// Output: [5, 6, 3, 4, 1, 2]
let arr = [[5, 6], [3, 4], [1, 2]]; let flatArray = arr.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue), []); console.log(flatArray); // Output: [5, 6, 3, 4, 1, 2]
let arr = [[5, 6], [3, 4], [1, 2]];

let flatArray = arr.reduceRight((accumulator, currentValue) => accumulator.concat(currentValue), []);
console.log(flatArray); 
// Output: [5, 6, 3, 4, 1, 2]

Example 4: Using reduceRight() Without an Initial Value

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
let numbers = [10, 20, 30];
let result = numbers.reduceRight((accumulator, currentValue) => accumulator - currentValue);
console.log(result); // Output: 0
let numbers = [10, 20, 30]; let result = numbers.reduceRight((accumulator, currentValue) => accumulator - currentValue); console.log(result); // Output: 0
let numbers = [10, 20, 30];

let result = numbers.reduceRight((accumulator, currentValue) => accumulator - currentValue);
console.log(result); // Output: 0

Supported Browsers

BrowserSupport
Chrome3+
Firefox3+
Safari4+
Edge12+
Opera10.5+
Internet Explorer9+
JavaScript, Web Technologies Tags:JavaScript-Array, JavaScript-Array-Method, JavaScript-Method

Post navigation

Previous Post: JavaScript Array reduce() Method
Next Post: JavaScript Array.from() Method

More Related Articles

JavaScript Array pop() Method JavaScript
JavaScript Array unshift() Method JavaScript
Copy Array by Value in JavaScript JavaScript
JavaScript Array reduce() Method JavaScript
How to Merge (Flatten) an Array of Arrays in JavaScript? JavaScript
JavaScript Array with() Method JavaScript

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Archives

  • May 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024

Categories

  • CSS
  • HTML
  • JavaScript
  • Lodash
  • PHP
  • Python
  • Uncategorized
  • Web Technologies
  • Web Templates

Recent Posts

  • How to Center Text in HTML?
  • Design a Simple HTML Page | First HTML Project
  • Best Way to Initialize an Empty Array in PHP
  • HTML Description Lists
  • HTML Ordered Lists

Recent Comments

No comments to show.

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme