Skip to main content

What Is the Difference Between Map,Filter,Reduce

Published: Asked By 1 Answers Answered
What does map() filter() reduce() function do in js.
Share:

1 Answer

M
Mufeeda Community Contributor Answered on

Map:-Transforms each element in an array and returns a new array with the transformed elements.

Ex:- const numbers = [1, 2, 3, 4];

const doubled = numbers.map(num => num * 2);

console.log(doubled); // Outputs: [2, 4, 6,

8]

 

Filter:-Filters elements in an array based on a test (provided as a function) and returns a new array with elements that pass the test.

Ex:- const numbers = [1, 2, 3, 4];

const evens = numbers.filter(num => num % 2 === 0);

console.log(evens); // Outputs: [2,

4]

Reduce:- Reduces an array to a single value by applying a function that accumulates a result over all elements of the array.

Ex:- const numbers = [1, 2, 3, 4];

const sum = numbers.reduce((total, num) => total + num, 0);

console.log(sum); // Outputs:

10

Your Answer

Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certifications

Earn certifications to enhance your resume.