JavaScript arrays are dynamic, indexed collections of values, used to store and manipulate data. They provide a variety of methods to perform operations such as searching, sorting, manipulating and transforming data.
Here are some of the most commonly used array methods in JavaScript, along with examples
push():
This method adds one or more elements to the end of an array and returns the new length of the array.
Example:
let array = [ 1, 2, 3 ];
array.push(4 , 5);
console.log(array); // [ 1, 2, 3 ,4 ,5 ]
pop():
This method removes the last element from an array and returns the removed element.
Example:
let array = [ 1, 2, 3 ];
let last = array.pop();
console.log(last); // 3
console.log(array); // [ 1, 2 ]
shift():
This method removes the first element from an array and returns the removed element.
Example:
let array = [1, 2, 3];
let firstElement = array.shift();
console.log(firstElement); // 1
console.log(array); // [ 2, 3 ]
unshift():
This method adds one or more elements to the beginning of an array and returns the new length of the array.
Example:
let array = [1, 2, 3];
array.unshift(0, -1);
console.log(array); // [ -1, 0, 1, 2, 3 ]
splice():
This method modifies an array by removing or replacing elements or adding new elements. It takes three arguments: the index at which to start changing the array, the number of elements to remove, and the elements to add.
Example:
let array = [1, 2, 3, 4, 5];
array.splice(2, 2, 6, 7);
console.log(array); // [1, 2, 6, 7, 5]
slice():
This method returns a shallow copy of a portion of an array, selected from start to end (end not included). The original array is not modified.
Example:
let array = [1, 2, 3, 4, 5];
let newArray = array.slice(1, 4);
console.log(newArray); // [2, 3, 4]
console.log(array); // [1, 2, 3, 4, 5]
concat():
This method returns a new array that is the result of concatenating the original array with one or more arrays or values. The original arrays are not modified.
Example:
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let newArray = array1.concat(array2);
console.log(newArray); // [1, 2, 3, 4, 5, 6]
console.log(array1); // [1, 2, 3]
console.log(array2); // [4, 5]
forEach():
This method executes a provided function once for each array element.
Example:
let array = [1, 2, 3, 4, 5];
array.forEach(function(element) {
console.log(element);
});
map():
This method creates a new array with the results of calling a provided function on every element in the calling array.
Example:
let array = [1, 2, 3, 4, 5];
let doubled = array.map(function(element) {
return element * 2;
});
console.log(doubled); // [2, 4, 6, 8, 10]
filter():
This method creates a new array with all elements that pass the test implemented by the provided function.
Example:
let array = [1, 2, 3, 4, 5];
let even = array.filter(function(element) {
return element % 2 === 0;
});
console.log(even); // [2, 4]
reduce():
This method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.
Example:
let array = [1, 2, 3, 4, 5];
let sum = array.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
console.log(sum); // 15
some():
This method tests whether at least one element in the array passes the test implemented by the provided function.
Example:
let array = [1, 2, 3, 4, 5];
let hasThree = array.some(function(element) {
return element === 3;
});
console.log(hasThree); // true
every():
This method tests whether all elements in the array pass the test implemented by the provided function.
Example:
let array = [1, 2, 3, 4, 5];
let allPositive = array.every(function(element) {
return element > 0;
});
console.log(allPositive); // true
sort():
This method sorts the elements of an array in place and returns the sorted array. The default sort order is built upon converting the elements into strings and then comparing their sequences of UTF-16 code unit values.
Example:
let array = [3, 1, 5, 2, 4];
array.sort();
console.log(array); // [1, 2, 3, 4, 5]
These are some of the most commonly used array methods in JavaScript. They provide a convenient and efficient way to manipulate and process arrays.
Thank you for Reading.