If you’ve started learning JavaScript, you’ve probably worked with arrays — those magical lists that hold multiple values. But knowing how to use arrays effectively means understanding the array functions that JavaScript gives us.
Let’s break down the most powerful and commonly used array functions that every developer should know.
📌 What is an Array?
An array is a special variable in JavaScript that can hold more than one value at a time:
const fruits = ['apple', 'banana', 'cherry'];
🚀 Common Array Functions You Should Know
1. .push()
– Add to the End
fruits.push('mango');
console.log(fruits); // ['apple', 'banana', 'cherry', 'mango']
2. .pop()
– Remove from the End
fruits.pop();
console.log(fruits); // ['apple', 'banana', 'cherry']
3. .shift()
– Remove from the Start
fruits.shift();
console.log(fruits); // ['banana', 'cherry']
4. .unshift()
– Add to the Start
fruits.unshift('kiwi');
console.log(fruits); // ['kiwi', 'banana', 'cherry']
🔄 Looping & Transforming Arrays
5. .forEach()
– Loop Through Every Element
fruits.forEach((fruit) => {
console.log(fruit);
});
6. .map()
– Create a New Array by Modifying Each Item
const upperCaseFruits = fruits.map((fruit) => fruit.toUpperCase());
console.log(upperCaseFruits); // ['KIWI', 'BANANA', 'CHERRY']
7. .filter()
– Return Items that Match a Condition
const shortFruits = fruits.filter((fruit) => fruit.length <= 5);
console.log(shortFruits); // ['kiwi']
8. .reduce()
– Turn an Array into a Single Value
const numbers = [10, 20, 30];
const total = numbers.reduce((acc, num) => acc + num, 0);
console.log(total); // 60
🔍 Searching and Testing
9. .find()
– Find First Match
const found = fruits.find((fruit) => fruit.startsWith('b'));
console.log(found); // 'banana'
10. .some()
/ .every()
– Test Conditions
console.log(fruits.some((fruit) => fruit === 'banana')); // true
console.log(fruits.every((fruit) => fruit.length > 2)); // true
✨ Bonus: Spread & Destructuring with Arrays
const moreFruits = [...fruits, 'grape'];
console.log(moreFruits); // ['kiwi', 'banana', 'cherry', 'grape']
const [first, second] = fruits;
console.log(first, second); // 'kiwi', 'banana'
🎯 Final Thoughts
JavaScript array functions are like superpowers. They make your code cleaner, more expressive, and much easier to work with. Whether you're building a small feature or a big app, these tools will save you time and effort.
Learn them. Practice them. Love them.
Top comments (2)
Nice article Shifa! 🌟 💯
thanks a lot