Common Array Methods in JavaScript
--
In this article we will see basic array methods used in our day-to-day programming.
1) array.push():
The push() method adds a new element to an array at the end.
Example:
var arrayPush = [1, 4, 5];
arrayPush.push(7); // [1,4,5,7]
2) array.pop():
The pop() method removes the last element from an array.
Example:
var arrayPop = [1, 4, 5, 7];
arrayPop.pop(); // [1,4,5]
3) array.sort():
The sort() method sorts an array alphabetically.
Example:
var arraySort = [“B”, “K”, “A”, “C”];
arraySort.sort(); // [A,B,C,K]
4) array.reverse():
The reverse() method reverses the elements in an array.
Example:
var arrayTest = [“B”, “K”, “A”, “C”];
arrayTest.sort(); // [A, B, C, K]
arrayTest.reverse(); // [K, C, B, A]
5) array.splice()
array.splice() method returns the removed item(s) in an array.
array.splice() method changes the original array.
array.splice() method can take n number of arguments
Example :
var array=[1,2,3,4,5];
console.log(array.splice(2)); // [3,4,5]
This will return [3,4,5]. The original array is affected resulting in array being [1,2].
//splice
var array=[1,2,3,4,5];
console.log(array.splice(2)); // [3,4,5]
console.log(array); // [1,2]
6) array.slice():
array.slice() method returns the selected element(s) in an array.
array.slice() method doesn’t change the original array.
array.slice() method takes 2 arguments.
Example:
var array=[1,2,3,4,5]
console.log(array.slice(2)); // [3,4,5]
This will return [3,4,5]. The original array is NOT affected with resulting in array being [1,2,3,4,5].
//slice
var array2=[1,2,3,4,5]
console.log(array2.slice(2)); // [3,4,5]
console.log(array2); // [1,2,3,4,5]
6) array.shift():
The array.shift() method removes the first element from an array and returns that removed element. Length of the array will be modified.
Example:
const array1 = [1, 2, 3];
const test = array1.shift();
console.log(array1); // [2, 3]
console.log(test); // [1]
7) array.unshift():
The array.unshift() method adds one or more elements to the beginning of an array and returns the new array.
Example:
const test = [1, 2, 3];
console.log(test.unshift(4, 5)); // [5]
console.log(test); // [4, 5, 1, 2, 3]
8) array.indexOf():
The array.indexOf() method returns the first index at which a given element can be found in the array. It returns -1 if it is not present.
Example:
const test = [‘a’, ‘b’, ‘c’, ‘d’, ‘b’];
console.log(test.indexOf(‘b’)); // 1
console.log(test.indexOf(‘b’, 2)); // 4
console.log(test.indexOf(‘z’)); // -1
9) array.length():
The length property of an object which is an instance of type Array sets or returns the number of elements in that array.
Example:
const test = [‘a’, ‘b’, ‘c’, ‘d’];
console.log(test.length); // 4
10) array.filter():
The array.filter() method returns a new array with all elements that pass the test implemented by the provided function.
Example:
const test = [‘abc’, ‘xyz’, ‘qwerty’, ‘xyzabc’, ‘jkl’, ‘zxc’];
const result = test.filter(word => word.length > 4);
console.log(result); // Array [“qwerty”, “xyzabc”]