Difference Between array.reduce() and reduceRight() in JavaScript
Last Updated :
19 Jul, 2024
The array.reduce() and array.reduceRight() are two array methods used for reducing an array to a single value. They are particularly useful for performing cumulative operations on the array elements.
Although they share similar functionality, their behaviour differs in the direction they iterate through the array. This article will explain the difference Between array.reduce() and reduceRight() in JavaScript.
What is array.reduce()?
The reduce()
method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
Syntax
array.reduce(callback(accumulator, currentValue, [currentIndex], [array]), [initialValue])
- callback: A function to execute on each element in the array, taking four arguments:
accumulator
: The accumulated value previously returned in the last invocation of the callback, or the initial value, if provided.currentValue
: The current element being processed in the array.currentIndex
(optional): The index of the current element being processed in the array.array
(optional): The array reduce
was called upon.
- initialValue (optional): A value to use as the first argument to the first call of the callback.
Features of array.reduce()
- Left-to-Right Iteration: Processes the array from the first element to the last.
- Accumulator: Utilizes an accumulator to hold the intermediate results of the reduction.
- Initial Value: Allows specifying an initial value for the accumulator.
- Flexible Callback: The callback function can take up to four arguments: accumulator, current value, current index, and the array itself.
- Chainable: Can be used in a chain of array methods for complex operations.
Example: Performing operation with array.reduce().
JavaScript
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reduce((accumulator, currentValue)
=> accumulator + currentValue, 0);
console.log(sum);
Output
15
What is reduceRight()?
The reduceRight()
method applies a function against an accumulator and each element in the array (from right to left) to reduce it to a single value.
Syntax
array.reduceRight(callback(accumulator, currentValue, [currentIndex], [array]), [initialValue])
- callback: A function to execute on each element in the array, taking four arguments:
accumulator
: The accumulated value previously returned in the last invocation of the callback, or the initial value, if provided.currentValue
: The current element being processed in the array.currentIndex
(optional): The index of the current element being processed in the array.array
(optional): The array reduceRight
was called upon.
- initialValue (optional): A value to use as the first argument to the first call of the callback.
Features of array.reduceRight()
- Right-to-Left Iteration: Processes the array from the last element to the first.
- Accumulator: Utilizes an accumulator to hold the intermediate results of the reduction.
- Initial Value: Allows specifying an initial value for the accumulator.
- Flexible Callback: The callback function can take up to four arguments: accumulator, current value, current index, and the array itself.
- Chainable: Can be used in a chain of array methods for complex operations.
Example: Performing operation with reduceRight().
JavaScript
const numbers = [1, 2, 3, 4, 5];
const reversedString = numbers.reduceRight((accumulator, currentValue)
=> accumulator + currentValue, '');
console.log(reversedString);
Output
54321
Difference Between array.reduce() and reduceRight() in Javascript
Characteristics | array.reduce() | reduceRight() |
---|
Execution Order | Left to right | Right to left |
---|
Initial Value | Optional starts with the first element | Optional starts with the last element |
---|
Use Case | Sequential operations | Reverse order operations |
---|
Empty Array | It Throws an error without an initial value | It Executes with or without initial value |
---|
Conclusion
Both reduce() and reduceRight() are powerful methods in JavaScript for reducing an array to the single value. Understanding their execution order and when to use each method based on the specific requirements of the application is crucial for the efficient array manipulation.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Difference Between IPv4 and IPv6 IPv4 and IPv6 are two versions of the system that gives devices a unique address on the internet, known as the Internet Protocol (IP). IP is like a set of rules that helps devices send and receive data online. Since the internet is made up of billions of connected devices, each one needs its own spe
7 min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
Difference between BFS and DFS Breadth-First Search (BFS) and Depth-First Search (DFS) are two fundamental algorithms used for traversing or searching graphs and trees. This article covers the basic difference between Breadth-First Search and Depth-First Search.Difference between BFS and DFSParametersBFSDFSStands forBFS stands fo
2 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read