All Important Array Methods in JavaScript

javascript array methods

Arrays are the type of data structure that are used to store multiple values at a time. We can access any element in the array by using its index value. For having operations on array methods are used. Here are some of the important array methods which are used in JavaScript:


It joins multiple or several arrays into one.

array1 = [1,2,3,4,5]array2 = [3,5,6,7]array1.concat(array2)
Result will be:[1, 2, 3, 4, 5, 3, 5, 6, 7]


It copies array elements within the array, to and from a specified position, and overwrites the original array. It takes 3 parameters which are:
target: index of the elements which are to be copied (required)
start: index position to start copying elements from
end: index up to which elements should be copied

array = ['apple', 'banana', 'cherries', 'Dates', 'Entawak', 'Fig']array.copyWithin(3,0,2)
Result will be:["apple", "banana", "cherries", "apple", "banana", "Fig"]


It returns the key/value pair of the array iteration object. We can repeat the 3rd step in the code as many times as we want until the array reaches the last element.

arr = ['bat','fish','cat','rat']values = arr.entries();;
Result will be:[0, "bat"]


It checks every element of an array passes the test. It returns the boolean value whether true or false.

var marks = [23,45,17,54,32]const marking = (mark)=> {return mark > 23}marks.every(marking)
Result will be:false


It fills the elements in an array with static values. It takes 3 values:
value: which is to be filled in array
start: from where the filling of element starts
end: up to which elements should be filled

array = [1,2,3,4,5,6,7,8]array.fill(9, 3, 7)
Result will be:[1, 2, 3, 9, 9, 9, 9, 8]


It creates a new array with every element of given array which passes the test.

arr = [2,6,9,13,24,8]const greater = arr.filter(arr => arr > 10);console.log(greater)
Result will be:[13, 24]


It returns the first element of the array that fulfills the given test condition.

const arr = [5, 2, 18, 13, 44];const num = arr.find(ele => ele > 10);console.log(num);
Result will be:18


It calls the given function for each element of an array.

const arr = ['ape', 'bat', 'camel'];arr.forEach(element => console.log(element));
Result will be:apebatcamel


It creates a new array from an iterable object.

console.log(Array.from('bestie'))console.log(Array.from([2,5],x=> x**2))
Result will be:["b", "e", "s", "t", "i", "e"][4, 25]


It checks whether an array consists of a specified element in it. It returns a boolean value whether true or not.

arr = ['cherry','banana','fig']arr.includes('kiwi')
Result will be:false


It returns primitive/index value of the specified object.

arr = ['bat','fish','cat']arr.indexOf('cat')
Result will be:2


It combines the elements of an array and converts them into a string.

arr = ['apple', 'banana', 'cherries']arr.join()
Result will be:"apple,banana,cherries"


It pushes/add the new element to the array.

arr = ['bat','fish','cat']arr.push('dog')console.log(arr)
Result will be:["bat", "fish", "cat", "dog"]


It pops/deletes the last element of the array.

arr = ['bat','fish','cat','rat']arr.pop()console.log(arr)
Result will be:["bat", "fish", "cat", "rat"]


It sorts the elements of an array in descending order.

arr = ['cat','rat','owl']arr.reverse()console.log(arr)
Result will be:["owl", "rat", "cat"]


It removes the starting element from the array.

arr = ['bat','fish','cat','monkey']arr.shift()console.log(arr)
Result will be:["fish", "cat", "monkey"]


It pull a copy of a portion of an array into a new array object.

arr = ['bat','fish','cat','rat','owl']console.log(arr.slice(3))console.log(arr.slice(2,5))console.log(arr.slice(1,5))
Result will be:["rat", "owl"]["cat", "rat", "owl"]["fish", "cat", "rat", "owl"]


It sorts the elements of an array in an alphabetical manner.

arr = ['cherry','fig','apple','olive']arr.sort()
Result will be:["apple", "cherry", "fig", "olive"]


It adds the element in a specified way and position in the array.

arr = ['Sun','Tue','Wed']arr.splice(1, 0, 'Mon')console.log(arr)
Result will be:["Sun", "Mon", "Tue", "Wed"]


It adds new element to the array from the beginning.

arr = ['fish','cat','rat']arr.unshift('camel')console.log(arr)
Result will be:["camel", "fish", "cat", "rat"]


Popular posts from this blog

These are the Best Online Python Courses For Beginners

These are the 5 Best Web Development Courses for Beginners

These are the Best C Courses Available Online