googleads
10 Useful JavaScript Array Methods You Must Know
preloder
Javascript FrameWorks Technical Web Development

10 Useful JavaScript Array Methods You Must Know

Basically, Javascript array method is one of the imperial factor in development that helps us to write more clean and better code. Collecting the list of useful array method will surely help you from any complex dilemma. This article is completely aimed to list down the 10 useful JavaScript array methods which you can utilize it in a better way.

Array.map()

map() method creates a new array with the result of calling a function on every element in an array. If you want to change all the elements in an array at the time then we can use the map() method.

For example converting all array elements in uppercase,

 

Array.filter()

filer() method creates a new array based on the array elements which passed certain conditions so if we want to filter out the specific element from the array then we can use this filter() method.

Let’s consider the following array,

 

From the fruits array, we can filter out fruit name which has the length more than 5.

 

Also Read:  3 Must Known Features Of JavaScript ECMAScript 6 (ES6)

 

Array.forEach()

forEach() method used for iterating array elements. It calls the provided callback function once in every array element & it is a modern way that used to write “for” loop.

Let’s consider the following array,

 

Array.indexOf()

indexOf() method is used to find the index of a given element in an array. If the element is duplicated, it returns the first index of an array so when an element is not found then it will return -1.

 

Array.every()

Every() method executes the provided function once for every element in an array until it finds the element where the callback function returns falsy value. If any element returns falsy value then every() method will immediately return false. Similarly, If all the element returns truthy values then every() method also will return true.

 

All the elements in the above array have more than 4 characters so when all the elements in the array returns truthy value, then it will return us the value “True”.

 

Now ‘apple’ element from an array will return falsy value, so immediately every() method also returns false so the moment it returns false this function will not be executed for other elements.

 

Array.splice()

Using splice() method we can remove and replace the existing element or even add a new element in the array. If the inserted element differs from the number of elements being deleted then the array length will be different at the end.

 

It will insert the grape element at first index of an array.

 

After inserting an element the array will look like this [“banana”, “grape”, “apple”, “mango”, “orange”], below code will help us to remove one element from index 2.

 

After removing an element the array will look like this [“banana”, “grape”, “mango”, “orange”],  now if we need to replace the index element 1 with apple then you can follow this, 

 

Array.includes()

includes() methods used to find a particular element in an array so if the given element is found in the array it will return true otherwise it will return false.

 

Best Read: 10 App Ideas Using Javascript That Turned Into Million-Dollar Startups

 

Array.push(), Array.unshift()

push() method adds one or more elements at the end of the array and it also returns the new length of an array.

 

unshift() method adds one or more elements at the beginning of the array and this will also return the new length of the array.

 

Array.pop(), Array.shift()

pop() method removes the last element of an array. Also, It changes the array length and it will return the removed element.

 

The shift() method removes the first element of an array and returns the removed element. It also changes the array.

 

Array.some()

some() method executes the provided function once for all the elements in an array until it finds the element in which callback function returns a truthy value. If any element returns a truthy value then some() method immediately returns true. Otherwise, it will return false.

 

Here the above array value ‘banana’ has a length of 6 so it will pass the following condition and returns true.

Now, some() method returns false value for all the elements returns false.

Hope you guys find it helpful. Try these array methods and let us know your result in the comment section.

Like What You're Reading ?

The following two tabs change content below.

Arjunan Subramani

Senior Software Engineer | Around 4 Years of experience in Web development and Testing arena. Plus, hands on expertise in Ruby, Ruby on Rails, Watir, Ruby Cucumber.

Leave a Reply

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

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call