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:

1.concat():

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]

2.copyWithin():

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"]

3.entries():

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();values.next().value;
Result will be:[0, "bat"]

4.every():

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

5.fill():

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]

6.filter():

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]

7.find():

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

8.forEach():

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

9.from():

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]

10.includes():

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

11.indexOf():

It returns primitive/index value of the specified object.

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

12.join():

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"

13.push():

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"]

14.pop():

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"]

15.reverse():

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"]

16.shift():

It removes the starting element from the array.

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

17.slice():

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"]

18.sort():

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"]

19.splice():

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"]

20.unshift():

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"]


Comments

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