Arrays In JavaScript
In JavaScript, an array is a type of data structure that is used to store a collection of values. An array is defined using square brackets, and its elements are separated by commas.
JavaScript provides many methods for working with arrays. Some of the most commonly used methods are:
push()
: adds one or more elements to the end of an array.pop()
: removes the last element from an array.shift()
: removes the first element from an array.unshift()
: adds one or more elements to the beginning of an array.slice()
: returns a new array containing a portion of an existing array.splice()
: adds or removes elements from an array at a specified index.concat()
: joins two or more arrays together.indexOf()
: returns the index of the first occurrence of a specified element in an array.lastIndexOf()
: returns the index of the last occurrence of a specified element in an array.forEach()
: executes a provided function once for each element in an array.map()
: creates a new array with the results of calling a provided function on every element in an array.filter()
: creates a new array with all elements that pass a provided test.reduce()
: applies a provided function to each element of an array and returns a single value.