In JavaScript, map, filter, and reduce are powerful
array methods that provide functional programming capabilities for handling arrays. They allow you to
transform, filter, and accumulate data concisely and
expressively.
map()
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
map(): Transforms each element in the array and returns a new array with the transformed elements.
filter(): Filters elements in the array based on a test and returns a new array with the elements that pass the test.
reduce(): Accumulates all elements in the array into a single value, typically used for
summing values, counting instances, or
combining elements in a specific way.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In JavaScript,
map,filter, andreduceare powerful array methods that provide functional programming capabilities for handling arrays. They allow you to transform, filter, and accumulate data concisely and expressively.map()The
map()method creates a new array populated with the results of calling a provided function on every element in the calling array.callback: Function that produces an element of the new array, taking three arguments:
thisArg (optional): Value to use as this when executing the callback.
Example
filter()The
filter()method creates a new array with all elements that pass the test implemented by the provided function.callback: Function that tests each element of the array. Return true to keep the element, false otherwise.
thisArg (optional): Value to use as this when executing the callback.
Example
reduce()The
reduce()method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.callback: Function to execute on each element in the array, taking four arguments:
initialValue (optional): Value to use as the first argument to the first call of the callback.
Example
Note of Uses:
map(): Transforms each element in the array and returns a new array with the transformed elements.filter(): Filters elements in the array based on a test and returns a new array with the elements that pass the test.reduce(): Accumulates all elements in the array into a single value, typically used for summing values, counting instances, or combining elements in a specific way.Read also,
What are JavaScript generators and how do they work?
What is the difference between null and undefined in JavaScript?
What are modules in JavaScript and how do you use them?
What is JavaScript's spread operator (...), and how is it used?