⬅ Previous Topic
JavaScript ArraysNext Topic ⮕
Destructuring Assignment in JavaScript⬅ Previous Topic
JavaScript ArraysNext Topic ⮕
Destructuring Assignment in JavaScriptJavaScript arrays come packed with powerful built-in methods that allow you to process, transform, and manipulate collections of data efficiently. These methods make code cleaner, shorter, and easier to maintain.
forEach()
- Loop Over Each ElementThe forEach()
method is used to execute a function on each item in the array.
const numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(num) {
console.log(num * 2);
});
Output:
2 4 6 8 10
Can forEach()
return a new array?
Answer: No, forEach()
does not return a new array. It only performs side effects like logging or updating variables.
map()
- Transform Each ElementThe map()
method creates a new array by transforming each element with a callback function.
const prices = [10, 20, 30];
const newPrices = prices.map(price => price + 5);
console.log(newPrices);
Output:
[15, 25, 35]
filter()
- Get Matching ItemsUse filter()
when you want to return a new array with only those elements that satisfy a condition.
const ages = [12, 17, 22, 30];
const adults = ages.filter(age => age >= 18);
console.log(adults);
Output:
[22, 30]
What happens if no elements match the condition?
Answer: filter()
returns an empty array if no elements pass the test.
reduce()
- Reduce to a Single Valuereduce()
processes each element and combines them into a single return value. Great for summing or building complex results.
const nums = [1, 2, 3, 4];
const sum = nums.reduce((acc, val) => acc + val, 0);
console.log(sum);
Output:
10
find()
- Return First Matchfind()
returns the first element that satisfies the given condition.
const users = [
{ id: 1, name: "Alice" },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" }
];
const result = users.find(user => user.id === 2);
console.log(result);
Output:
{ id: 2, name: "Bob" }
includes()
- Check for Existenceincludes()
checks if a value exists in an array and returns true or false.
const fruits = ["apple", "banana", "mango"];
console.log(fruits.includes("banana")); // true
console.log(fruits.includes("grape")); // false
Output:
true false
push()
and pop()
- Add/Remove from Endpush()
adds an item to the end; pop()
removes the last item.
const stack = [1, 2];
stack.push(3); // [1, 2, 3]
stack.pop(); // returns 3, stack becomes [1, 2]
console.log(stack);
Output:
[1, 2]
shift()
and unshift()
- Add/Remove from Startunshift()
adds to the beginning; shift()
removes the first element.
const queue = [2, 3];
queue.unshift(1); // [1, 2, 3]
queue.shift(); // returns 1, queue becomes [2, 3]
console.log(queue);
Output:
[2, 3]
Mastering array methods makes you a much more effective JavaScript developer. These functions are essential when working with lists of data and are often used in interviews and real-world apps.
map()
and forEach()
?pop()
on an empty array?⬅ Previous Topic
JavaScript ArraysNext Topic ⮕
Destructuring Assignment in JavaScriptYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.