The map() method transforms each element in an array and returns a new array with the modified values.
Code:
const numbers = [1, 2, 3, 4]; const doubled = numbers.map(num => num * 2); console.log(doubled); // [2, 4, 6, 8]
The filter() method creates a new array with elements that pass a specific condition.
Code:
const numbers = [1, 2, 3, 4, 5]; const evenNumbers = numbers.filter(num => num % 2 === 0); console.log(evenNumbers); // [2, 4]
The reduce() method reduces an array to a single value by applying a function repeatedly.
Code:
const numbers = [1, 2, 3, 4]; const sum = numbers.reduce((acc, curr) => acc + curr, 0); console.log(sum); // 10
Mastering these three methods will make your JavaScript code more concise, readable, and powerful.
Happy Coding! 🚀