Array Methods in JavaScript
In this article we will explore about array methods in JavaScript!
Table of contents
First, let's understand what an array is before exploring more about array methods.
What is Array?
An Array object in JavaScript is used to store a collection of multiple items under a single variable name. We can fetch any array element with the help of array indices.
Example of an array in JavaScript:
let arr = [1, "Apple", "Banana", true];
Array Methods
length
: This method is used to get the length of an array i.e. how many elements are present in an array.push()
: This method is used to insert one or more elements in an array. The elements are added at the end of an array.pop()
: This method is used to remove an element from an element. The element is removed from the last.slice()
: This method is used to get a portion of the array according to the value of the index provided in the method. The ending index given in the method is not included.splice()
: This method is used to remove or replace an existing element of the array and/or add a new element in its place.concat()
: This method is used to merge or add two or more arrays. It returns a new array after merging.fill()
: This method is used to change all the elements in the array to a static value, from a start index to an end index and returns the modified array.includes()
: This method is used to determine whether a certain value is present in an array or not.indexOf()
: This method is used to get the first index at which a particular element is present in an array.isArray()
: This method is used to check whether the provided value is an array or not.join()
: This method is used to concatenate all elements of an array. It creates and returns a new string.keys()
: This method is used to create an iterator object that contains only the keys of the array.lastIndexOf()
: This method is used to get the last index at which the given element can be found.map()
: This method is used to call the specified function for every array element and return the new array.reverse()
: This method is used to reverse the elements of the given array.shift()
: This method is used to remove the first element of an array and returns the removed element.unshift()
: This method is used to add one or more elements at the beginning of the array and it returns the new array length.sort()
: This method is used to sort the elements of an array. The default sort order is ascending.split()
: This method is used to convert a string to an array.
Thanks for reading ๐