Top JavaScript array methods you should know

Jamil Moughal
2 min readJul 17, 2020

JavaScript is a lightweight interpreted and just-in-time compiled programming language. It is one of the most widely used programming language for web pages. It is used by almost every web developer who is working on front-end.

In JavaScript, array is a single variable that is used to store different elements. It is often used when we want to store list of elements and access them by a single variable. Unlike most languages where array is a reference to the multiple variable, in JavaScript array is a single variable that stores multiple elements.

forEach(): Foreach method can help you to loop over every item in the array:

var array = [1,2,3,4,5,6,7,8,9,10]array.forEach(item=>{
console.log(item*2)
});
//Output 2 4 6 8 10 12 14 16 18 20

includes(): Includes methods in similar to Contains method in C# lists. It will return a boolean value. i.e true if a element exists in the arrar and false if element doesn’t exists.

let array = ["one","two","three"]array.includes("one") //Output truearray.includes("four") //Output false

filter(): This method receives a function as a parameter. And return a new array that contains all the elements of the array for which the filtering function passed as argument returns true.

var persons = [
{ Id:1, name:"Alex" },
{ Id:2, name:"John" },
{ Id:3, name:"Alex" },
{ Id:4, name:"Maria" }
]
persons.filter(item=>item.name==="Alex")//Output
{Id:1, name:"Alex"},
{Id:3, name:"Alex"}

findIndex(): This method is used to find index of some element in the array. Let say we want to know what is the index of a specific element in the array then this method will be used.

const cities =[
{ Id:1, name:"New York" },
{ Id:2, name:"Munich" },
{ Id:3, name:"Vienna" },
{ Id:4, name:"Calgary"}
]
cities.findIndex(city=>city.name==="Munich")
//Output will be 1
cities.findIndex(city=>city.name==="Calgary")
//Output will be 3

sort(): Whenever you want to sort some array sort() method is there for you to sort array. This method receives a function as a parameter and sort elements of array and returns array.

let integers_array = [1,3,6,2,8,]integers_array.sort((a,b)=>b-a)
//Output [8, 6, 3, 2, 1]
integers_array.sort((a,b)=>a-b)
//Output [1, 2, 3, 6, 8]

reduce(): When you want to apply a operation on all elements of an array and store result in a single variable then reduce() method will help you.

let numbers = [1,2,3,4,5,6]const sum = numbers.reduce((total,value) => total+value,0)
//Output sum=21
const multiply = numbers.reduce((total,value) => total*value,1)
//Output multiply=720

every(): This method is used to iterate thorough all array elements and return a boolean value depending on the condition given to method.

const elements =[1,2,3,4,5]elements.every(x=>x<10)
//Output true
elements.every(x=>x<4)
//Output false

--

--

Jamil Moughal

Software Engineer | Cloud (Azure, AWS) | System Design | Data and AI Enthusiast | Content Creator | Lifelong learner