JavaScript Array Methods with Practical Examples

sudhanshu_developer

Sudhanshu Gaikwad

Posted on July 5, 2024

JavaScript Array Methods with Practical Examples

In JavaScript, an array is a data structure that stores multiple values in a single variable. The power of JavaScript arrays comes from their built-in methods. These methods are functions that perform various operations on arrays, saving us from writing common functions from scratch. Each method has a unique purpose, such as transforming, searching, or sorting the array.

1. concat()
Combines two or more arrays.

let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let combined = arr1.concat(arr2);
console.log(combined); // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

2. every()
Tests whether all elements pass the provided function.

let arr = [1, 2, 3, 4, 5];
let allPositive = arr.every(num => num > 0);
console.log(allPositive); // true

Enter fullscreen mode Exit fullscreen mode

3. filter()
Creates a new array with all elements that pass the provided function.

let arr = [1, 2, 3, 4, 5];
let evenNumbers = arr.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]

Enter fullscreen mode Exit fullscreen mode

4. find()
Returns the first element that satisfies the provided function.

let arr = [1, 2, 3, 4, 5];
let found = arr.find(num => num > 3);
console.log(found); // 4

Enter fullscreen mode Exit fullscreen mode

5. findIndex()
Returns the index of the first element that satisfies the provided function.

let arr = [1, 2, 3, 4, 5];
let index = arr.findIndex(num => num > 3);
console.log(index); // 3

Enter fullscreen mode Exit fullscreen mode

6. forEach()
Executes a provided function once for each array element.

let arr = [1, 2, 3, 4, 5];
arr.forEach(num => console.log(num)); // 1 2 3 4 5

Enter fullscreen mode Exit fullscreen mode

7. includes()
Determines if an array contains a certain element.

let arr = [1, 2, 3, 4, 5];
let hasThree = arr.includes(3);
console.log(hasThree); // true

Enter fullscreen mode Exit fullscreen mode

8. indexOf()
Returns the first index at which a given element can be found.

let arr = [1, 2, 3, 4, 5];
let index = arr.indexOf(3);
console.log(index); // 2

Enter fullscreen mode Exit fullscreen mode

9. join()
Joins all elements into a string.

let arr = [1, 2, 3, 4, 5];
let str = arr.join('-');
console.log(str); // "1-2-3-4-5"

Enter fullscreen mode Exit fullscreen mode

10. map()
Creates a new array with the results of calling a provided function on every element.

let arr = [1, 2, 3, 4, 5];
let squared = arr.map(num => num * num);
console.log(squared); // [1, 4, 9, 16, 25]

Enter fullscreen mode Exit fullscreen mode

11. pop()
Removes the last element and returns it.

let arr = [1, 2, 3, 4, 5];
let last = arr.pop();
console.log(last); // 5
console.log(arr); // [1, 2, 3, 4]

Enter fullscreen mode Exit fullscreen mode

12. push()
Adds one or more elements to the end and returns the new length.

let arr = [1, 2, 3, 4];
arr.push(5);
console.log(arr); // [1, 2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

13. reduce()
Executes a reducer function on each element, resulting in a single output value.

let arr = [1, 2, 3, 4, 5];
let sum = arr.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
console.log(sum); // 15

Enter fullscreen mode Exit fullscreen mode

14. reverse()
Reverses the array in place.

let arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr); // [5, 4, 3, 2, 1]

Enter fullscreen mode Exit fullscreen mode

15. shift()
Removes the first element and returns it.

let arr = [1, 2, 3, 4, 5];
let first = arr.shift();
console.log(first); // 1
console.log(arr); // [2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

16. slice()
Returns a shallow copy of a portion of an array into a new array.

let arr = [1, 2, 3, 4, 5];
let sliced = arr.slice(1, 3);
console.log(sliced); // [2, 3]

Enter fullscreen mode Exit fullscreen mode

17. some()
Tests whether at least one element passes the provided function.

let arr = [1, 2, 3, 4, 5];
let hasEven = arr.some(num => num % 2 === 0);
console.log(hasEven); // true

Enter fullscreen mode Exit fullscreen mode

18. sort()
Sorts the elements in place.

let arr = [5, 2, 1, 4, 3];
arr.sort();
console.log(arr); // [1, 2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

19. splice()
Changes the contents by removing or replacing existing elements and/or adding new elements.

let arr = [1, 2, 3, 4, 5];
arr.splice(2, 1, 'a', 'b');
console.log(arr); // [1, 2, 'a', 'b', 4, 5]

Enter fullscreen mode Exit fullscreen mode

20. toString()
Returns a string representing the array.

let arr = [1, 2, 3, 4, 5];
let str = arr.toString();
console.log(str); // "1,2,3,4,5"

Enter fullscreen mode Exit fullscreen mode

21. unshift()
Adds one or more elements to the beginning and returns the new length.

let arr = [2, 3, 4, 5];
arr.unshift(1);
console.log(arr); // [1, 2, 3, 4, 5]

Enter fullscreen mode Exit fullscreen mode

22. flat()
Creates a new array with all sub-array elements concatenated into it.

let arr = [1, 2, [3, 4], [5, 6]];
let flattened = arr.flat();
console.log(flattened); // [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

23. flatMap()
First maps each element using a mapping function, then flattens the result into a new array.

let arr = [1, 2, 3];
let mapped = arr.flatMap(num => [num, num * 2]);
console.log(mapped); // [1, 2, 2, 4, 3, 6]

Enter fullscreen mode Exit fullscreen mode

24. from()
Checks if the passed value is an array.

let str = 'hello';
let arr = Array.from(str);
console.log(arr); // ['h', 'e', 'l', 'l', 'o']

Enter fullscreen mode Exit fullscreen mode

25. isArray(
Checks if the passed value is an array.

console.log(Array.isArray([1, 2, 3])); // true
console.log(Array.isArray('hello')); // false

Enter fullscreen mode Exit fullscreen mode

26. of()
Creates a new array instance with a variable number of arguments.

let arr = Array.of(1, 2, 3);
console.log(arr); // [1, 2, 3]

Enter fullscreen mode Exit fullscreen mode

If you need more detailed explanations or additional methods, feel free to ask!

💖 💪 🙅 🚩
sudhanshu_developer
Sudhanshu Gaikwad

Posted on July 5, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related