36 Important JavaScript Array Methods With Examples

24 JavaScript Array Methods That You Should Know

Hey! Coders. In this post, we are going to see 5+ Array Methods With Examples That You Should Know In JavaScript. Arrays are most often used while creating projects with Js, they are most useful when we have to handle the collection of data.

These array methods are useful to start development with react, making CURD API & designing algorithms in JavaScript. Understanding Array methods can help you to write better & efficient programs.

Also Read: 24 JavaScript Object Methods That You Should Know

This tutorial focuses on vital array methods like Map, Filter, Reduce, Sort, etc. We will walk through each concept with examples and try to understand how to use them effectively.

36 Array Methods That You Should Know In JavaScript

Use CaseMethod Names
Creating Array :Array()
Array.of()
Array.from()
Add Or Remove Elements Of Array : Push()
pop()
shift()
unshift()
concat()
splice()
Accessing the Elements Of The Array: at()
Searching & Filtering Array Methods: IndexOf()
lastIndexOf()
includes()
find()
findIndex()
filter()
Iterating: forEach()
map()
reduce()
reduceRight()
Sorting & Reording JavaScript Array Methods:sort()
reverse()
copyWithin()
fill()
Other Javascript Array Methods Methods :slice()
join()
toString()
toLocaleString()
isArray()
Operations On Subarrays :flat()
flatMap()
Advanced Javascript Array Methods For Manipulations :every()
some()
keys()
values () & entries()

Also Read: How To Make CRUD APIs With MongoDB In Express

Array Methods For Creating & Initializing Arrays

let arr = new Array(10);
let arr = Array.of(1,2,3,4); 
let arr = Array.from('hello'); // ['h', 'e', 'l', 'l', 'o']

Methods To Add Or Remove Elements

let arr = [1, 2];
arr.push(3); // [1, 2, 3]
let arr = [1, 2, 3];
arr.pop(); // [1, 2]
let arr = [2, 3];
arr.unshift(1); // [1, 2, 3]
let arr = [1, 2, 3];
arr.shift(); // [2, 3]
let arr = [1, 2, 3];
arr.splice(1, 1, 'a'); // [1, 'a', 3]

In the above code, we have started from index 1 as the first parameter of splice wants where to start. Now Splice wants how many elements to remove from the start point & third parameter adds an element to that array from the start index.

let arr1 = [1,2];
let arr2 = [3,4];
let newarr = arr1.concat(arr2);
//[1,2,3,4]

Methods To Accessing The Elements Of Array

let arr = [1,2,3]
arr.at(1)
//2

Methods For Searching & Filtering Of Array

let arr = [1, 2, 3];
arr.indexOf(2); // 1
let arr = [1, 2, 2, 3];
arr.lastIndexOf(2); // 2
let arr = [1, 2, 3];
arr.includes(2); // true
let arr = [1, 2, 3];
arr.find(el => el > 1); // 2
let arr = [1, 2, 3];
arr.findIndex(el => el > 1); // 1
let arr = [1, 2, 3, 4];
arr.filter(el => el > 2); // [3, 4]

Methods To Iterate Through An Array:

let arr = [1, 2, 3];
arr.forEach(el => console.log(el)); // Logs 1, 2, 3
let arr = [1, 2, 3];
let newArr = arr.map(el => el * 2); // [2, 4, 6]
let arr = [1, 2, 3];
let sum = arr.reduce((acc, el) => acc + el, 0); // 6
let arr = [1, 2, 3];
let sum = arr.reduceRight((acc, el) => acc + el, 0); // 6

Methods For Sorting & Reordering An Array:

let arr = [3, 1, 2];
arr.sort(); // [1, 2, 3]
let arr = [1, 2, 3];
arr.reverse(); // [3, 2, 1]
let arr = [1, 2, 3, 4, 5, 6];
arr.copyWithin(0, 2, 4); // [3, 4, 3, 4, 5, 6]
let arr = [1, 2, 3];
arr.fill(0); // [0, 0, 0]

Other Useful Methods Of An Array:

let arr = [1, 2, 3];
let newArr = arr.slice(1); // [2, 3]
let arr = ['a', 'b', 'c'];
arr.join('-'); // 'a-b-c'
let arr = [1, 2, 3];
arr.toString(); // '1,2,3'
let arr = [1, 'test', new Date()];
arr.toLocaleString(); // '1,test,6/14/2024'
Array.isArray([1, 2, 3]); // true

Array Methods That Help To Working With Subarrays:

let arr = [1, [2, [3, [4]]]];
arr.flat(2); // [1, 2, 3, [4]]
let arr = [1, 2, 3];
arr.flatMap(x => [x, x * 2]); // [1, 2, 2, 4, 3, 6]

Advanced Array Methods For Array Manipulations:

let arr = [1, 2, 3];
arr.every(el => el > 0); // true
let arr = [1, 2, 3];
arr.some(el => el > 2); // true
let arr = [1, 2, 3];
for (let key of arr.keys()) {
  console.log(key); // Logs 0, 1, 2
}
let arr = [1, 2, 3];
for (let value of arr.values()) {
  console.log(value); // Logs 1, 2, 3
}
let arr = [1, 2, 3];
for (let entry of arr.entries()) {
  console.log(entry); // Logs [0, 1], [1, 2], [2, 3]
}

Conclusion On 36 JavaScript Array Methods That You Should Know

I Hope That You Liked these important methods of JavaScript Array Functions. All the above methods can be combined with each other to create complex manipulations and transformations on the arrays. This combination can help to write better algorithms making absolutely powerful & versatile for handling data in our code.


Last Updated: August 11, 2024

By JSM Hemant

About Author

Hello, Myself Hemant. I Am Web Developer & Likes To Create Awesome Projects By Using MERN, Java, Python, C++. In This Website You Will Get Cool Projects Made With Above Technologies With Full Source Code. You Can Also Follow This Website On Author Social Media:

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Recent Posts