Array Methods
Array methods are built-in functions that allow various operations on arrays, such as adding, removing, or transforming elements. Here are some of the most commonly used array methods:
1. push()
Adds one or more elements to the end of an array and returns the new length of the array.
let fruits = ['Apple', 'Banana'];let res=fruits.push('Cherry','Orange');console.log('Fruits: '+fruits);console.log('Length of new array: '+res);/*OutputFruits: Apple,Banana,Cherry,OrangeLength of new array: 4*/
2. pop()
Removes the last element from an array and returns that element.
let fruits = ['Apple', 'Banana','Cherry','Orange'];let res=fruits.pop();console.log('Fruits: '+fruits);console.log('Removed Fruits: '+res);/*OutputFruits: Apple,Banana,CherryRemoved Fruits: Orange*/
3. unshift()
Adds one or more elements to the beginning of an array and returns the new length of the array.
let fruits = ['Apple', 'Banana'];let res=fruits.unshift('Cherry','Orange');console.log('Fruits: '+fruits);console.log('Length of the new array: '+res);/*OutputFruits: Cherry,Orange,Apple,BananaLength of the new array: 4*/
4. shift()
Removes the first element from an array and returns that element.
let fruits = ['Apple', 'Banana','Cherry','Orange'];let res=fruits.shift();console.log('Fruits: '+fruits);console.log('Removed Element: '+res);/*OutputFruits: Banana,Cherry,OrangeRemoved Element: Apple*/
5. sort()
The sort method is used to sort the elements of an array in place, meaning it changes the original array and returns the sorted array.
let arr = [3, 1, 4, 2];arr.sort();console.log(arr);/*Output:[ 1, 2, 3, 4 ]*/
6. reverse()
The reverse method is used to reverse the order of elements in an array in place. This method modifies the original array and returns the reversed array.
let arr = [1, 2, 3,4];arr.reverse();console.log(arr);/*Output:[ 4, 3, 2, 1 ]*/
7. concat()
The concat method is specifically designed to merge arrays. It returns a new array and does not modify the original arrays.
let arr1 = [1, 2, 3];let arr2 = [4, 5, 6];let mergedArr = arr1.concat(arr2);console.log(mergedArr);/*Output:[ 1, 2, 3, 4, 5, 6 ]*/
You can also concatenate more than two arrays at once.
let arr1 = [1, 2, 3];let arr2 = [4, 5, 6];let arr3 = [7, 8, 9];let mergedArr = arr1.concat(arr2,arr3);console.log(mergedArr);/*Output:[1, 2, 3, 4, 5,6, 7, 8, 9]*/
8. Spread Operator (...)
The spread operator (...) is a more modern and concise way to merge arrays. It creates a new array by spreading the elements of existing arrays.
let arr1 = [1, 2, 3];let arr2 = [4, 5, 6];let mergedArr = [...arr1, ...arr2];console.log(mergedArr);/*Output:[1, 2, 3, 4, 5, 6]*/
This method also works for merging more than two arrays.
let arr1 = [1, 2, 3];let arr2 = [4, 5, 6];let arr3 = [7, 8, 9];let mergedArr = [...arr1, ...arr2, ...arr3];console.log(mergedArr);/*Output:[1, 2, 3, 4, 5, 6, 7, 8, 9]*/
9. splice()
The splice method in JavaScript/Node is a powerful way to add, remove, and replace elements in an array. It changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
⮚ read more about the splice method
Syntax
array.splice(start, deleteCount, item1, item2, ...);
Examples
let arry=['a','b','c','d'];let res=arry.splice(1,1,'x','y'); // starts at index 1, removes 1 element (b), add x, yconsole.log(arry); //array after adding x, y [ 'a', 'x', 'y', 'c', 'd' ]console.log(res); //removed element [b]/*Output[ 'a', 'x', 'y', 'c', 'd' ][ 'b' ]*/
10. slice()
The slice method in JavaScript is used to extract a portion of an array into a new array without modifying the original array. The slice method returns a shallow copy of a portion of an array into a new array object selected from start to end (end not included). The original array will not be modified.
Syntax:
array.slice(start, end);
Example
let arr = ['a', 'b', 'c', 'd', 'e'];let slicedArr = arr.slice(1, 3); // Slices from index 1 to 3 (3 not included)console.log(slicedArr); // ['b', 'c']console.log(arr); // ['a', 'b', 'c', 'd', 'e'] (original array is not modified)/*Output[ 'b', 'c' ][ 'a', 'b', 'c', 'd', 'e' ]*/
11. indexOf
Returns the first index at which a given element can be found in the array, or -1 if it is not present.
let arr = ['a', 'b', 'c', 'd', 'e'];let indexOfC = arr.indexOf('c');let indexOfG = arr.indexOf('g');console.log(indexOfC); //2console.log(indexOfG); // -1 (g not present in the array)/*Output2-1*/
12. includes
The includes method determines whether an element exists in an array or not. It returns true if the element is found, and false otherwise. It is case-sensitive. This means it will only find a string in the array if the case matches exactly.
let arr = ['apple', 'banana', 'Cherry'];console.log(arr.includes('apple')); // Output: trueconsole.log(arr.includes('Banana')); // Output: false (case-sensitive)/*Outputtruefalse*/
These methods offer a comprehensive toolkit for manipulating arrays in JavaScript. Mastering these methods allows for the effective performance of a wide range of array operations, making it easier to handle various programming tasks efficiently.