DEV Community

Shifa
Shifa

Posted on

10 JavaScript Array Functions Every Dev Must Know (or Risk Falling Behind)

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'];
Enter fullscreen mode Exit fullscreen mode

🚀 Common Array Functions You Should Know

1. .push() – Add to the End

fruits.push('mango');
console.log(fruits); // ['apple', 'banana', 'cherry', 'mango']
Enter fullscreen mode Exit fullscreen mode

2. .pop() – Remove from the End

fruits.pop();
console.log(fruits); // ['apple', 'banana', 'cherry']
Enter fullscreen mode Exit fullscreen mode

3. .shift() – Remove from the Start

fruits.shift();
console.log(fruits); // ['banana', 'cherry']
Enter fullscreen mode Exit fullscreen mode

4. .unshift() – Add to the Start

fruits.unshift('kiwi');
console.log(fruits); // ['kiwi', 'banana', 'cherry']
Enter fullscreen mode Exit fullscreen mode

🔄 Looping & Transforming Arrays

5. .forEach() – Loop Through Every Element

fruits.forEach((fruit) => {
  console.log(fruit);
});
Enter fullscreen mode Exit fullscreen mode

6. .map() – Create a New Array by Modifying Each Item

const upperCaseFruits = fruits.map((fruit) => fruit.toUpperCase());
console.log(upperCaseFruits); // ['KIWI', 'BANANA', 'CHERRY']
Enter fullscreen mode Exit fullscreen mode

7. .filter() – Return Items that Match a Condition

const shortFruits = fruits.filter((fruit) => fruit.length <= 5);
console.log(shortFruits); // ['kiwi']
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🔍 Searching and Testing

9. .find() – Find First Match

const found = fruits.find((fruit) => fruit.startsWith('b'));
console.log(found); // 'banana'
Enter fullscreen mode Exit fullscreen mode

10. .some() / .every() – Test Conditions

console.log(fruits.some((fruit) => fruit === 'banana')); // true
console.log(fruits.every((fruit) => fruit.length > 2));  // true
Enter fullscreen mode Exit fullscreen mode

✨ 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'
Enter fullscreen mode Exit fullscreen mode

🎯 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)

Collapse
 
davinceleecode profile image
davinceleecode

Nice article Shifa! 🌟 💯

Collapse
 
shifa_2 profile image
Shifa

thanks a lot