In JavaScript, arrays are used to store multiple values in a single variable. They are used to group related data items and are a commonly used data structure. JavaScript provides many built-in methods for manipulating arrays, making it easy to perform operations like adding or removing elements, sorting, and searching.
In this article, we will explore some of the most commonly used array methods in JavaScript.
push() Method
The push() method adds one or more elements to the end of an array and returns the new length of the array.
Example
let fruits = ["apple", "banana", "orange"];
fruits.push("kiwi", "mango");
console.log(fruits); // Output: ["apple", "banana", "orange", "kiwi", "mango"]
pop() Method
The pop() method removes the last element from an array and returns that element.
Example
let fruits = ["apple", "banana", "orange"];
fruits.
();
console.log(fruits); // Output: ["apple", "banana"]
shift() Method
The shift() method removes the first element from an array and returns that element.
Example
let fruits = ["apple", "banana", "orange"];
fruits.shift();
console.log(fruits); // Output: ["banana", "orange"]
unshift() Method
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
Example
let fruits = ["apple", "banana", "orange"];
fruits.unshift("kiwi", "mango");
console.log(fruits); // Output: ["kiwi", "mango", "apple", "banana", "orange"]
slice() Method
The slice() method returns a new array with a portion of an existing array.
Example
let fruits = ["apple", "banana", "orange", "kiwi", "mango"];
let citrus = fruits.slice(2, 4);
console.log(citrus); // Output: ["orange", "kiwi"]
splice() Method
The splice() method adds/removes elements to/from an array.
Example
let fruits = ["apple", "banana", "orange", "kiwi", "mango"];
fruits.splice(2, 1, "pear");
console.log(fruits); // Output: ["apple", "banana", "pear", "kiwi", "mango"]
concat() Method
The concat() method is used to join two or more arrays and returns a new array.
Example
let fruits1 = ["apple", "banana", "orange"];
let fruits2 = ["kiwi", "mango"];
let fruits = fruits1.concat(fruits2);
console.log(fruits); // Output: ["apple", "banana", "orange", "kiwi", "mango"]
join() Method
The join() method returns a string with all the array elements joined.
Example
let fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("demo").innerHTML = fruits.join(" * ");
// Output: Banana * Orange * Apple * Mango
map(): This method creates a new array by calling a function on each element of the original array.
filter(): This method creates a new array by filtering out elements that don’t pass a test function.
reduce(): This method applies a function to each element of an array to reduce it to a single value.
sort(): This method sorts the elements of an array in place and returns the sorted array.
reverse(): This method reverses the order of the elements in an array in place and returns the reversed array.
indexOf(): This method returns the first index at which a given element can be found in the array, or -1 if it is not present.
lastIndexOf(): This method returns the last index at which a given element can be found in the array, or -1 if it is not present.
includes(): This method determines whether an array includes a certain element, returning true or false as appropriate.
every(): This method tests whether all elements in the array pass the test implemented by the provided function, returning true or false as appropriate.
some(): This method tests whether at least one element in the array passes the test implemented by the provided function, returning true or false as appropriate.
forEach(): This method executes a provided function once for each array element.
find(): This method returns the value of the first element in the array that satisfies the provided testing function.
findIndex(): This method returns the index of the first element in the array that satisfies the provided testing function, or -1 if no such element is found.
from(): This method creates a new, shallow-copied array instance from an array-like or iterable object.
isArray(): This method determines whether the passed value is an Array.