In this tutorial, you will learn JavaScript Array Methods including examples.
Sometimes, when you work with Javascript arrays, these methods help you write clean and less effort js script/code.
The following array methods in javascript:
The javascript pop() method is used to remove the last element from an array.
The method returns the value of the removed item.
var lang = ["PHP", "Python", "Java", "C"]; lang.pop();
Result of the above example is:
Output
// New array: PHP, Python, Java
The js push() method is used to add a new element to the end from an array.
var lang = ["PHP", "Python", "Java", "C"]; lang.push("JavaScript");
Result of the above example is:
Output
// New array: PHP, Python, Java, C, JavaScript
The js toString() method is used to convert array to string.
var lang = ["PHP", "Python", "Java", "C"]; lang.toString();
Result of the above example is:
// output
PHP, Python, Java, C
The javascript join() method is used to join the elements of an array into a string.
In otherwords, The "join()" method puts all the elements of the array into a string list. This method difference from "toString()" method.
var lang = ["PHP", "Python", "Java", "C"]; lang.join(" - ");
Result of the above example is:
// output
PHP - Python - Java - C
The javascript splice() method is used to add and remove items from an array.
syntax
================
array.splice(index, howMany, [element1][, …, elementN]);
var lang = ["PHP", "Python", "Java", "C"]; lang.splice(2, 0, "Javascript", "Rust");
Result of the above example is:
// Output
new array: PHP, Python, Javascript, Rust, Java, C
The javascript array sort() method either alphabetic or numeric sorts an array.
var lang = ["PHP", "Python", "Java", "C"]; lang.sort();
Result of the above example is:
// Outuput
array: C, Java, PHP, Python
The javascript shift() method is used to remove the first element from an array.
var lang = ["PHP", "Python", "Java", "C"]; lang.shift();
Result of the above example is:
// Output
new array: Python, Java, C
The js unshift() method adds a new element to the beginning of an array.
var lang = ["PHP", "Python", "Java", "C"]; cars.unshift("JavaScript");
Result of the above example is:
//Output
New array: JavaScript, PHP, Python, Java, C
The method is used for reverses the order of the elements in an array.
var lang = ["PHP", "Python", "Java", "C"]; lang.reverse();
Result of the above example is:
// Outuput
array: C, Java, PHP, Python
The javascript concat() method joins two or more arrays and makes a new one.
var lang = ["PHP", "Python", "Java", "C"]; var newlang = ["JavaScript", "Rust"]; var join = lang.concat(newlang);
Result of the above example is:
// Output
New array: PHP, Python, Java, C, JavaScript, Rust
The js slice() array() method is used to selected elements in an array and make a new one. It can take one or two arguments.
var lang = ["PHP", "Python", "Java", "C", "JavaScript"]; var lang = cars.slice(1, 4);
Result of the above example is:
//Output
New Array : Python, JavaScript
The js filter() method is used to filter the array, according our conditions.
<p>function isCheck(value) {<br> return value < 10;<br> } var filter = [10, 5, 16, 4, 7, 12].filter(isCheck);
Result of the above example is:
Output
//new array: 5,4,7
In this example the function filter() creates a new array. Those elements that satisfy the condition checked by isCheck() function.
The js find() method is used find the first element of an array.
function isCheck(value) { return value >= 10; } var find= [10, 5, 16, 4, 7, 12].find(isCheck);
Result of the above example is:
Output
// 12
The JavaScript Array forEach method permits the call of a function on each element of an array in an easy way.
var num = [18, 12, 10, 15]; num.forEach(function(item) { document.writeln(item); });
Result of the above example is:
Output
// return
18 12 10 15
The js map() method, creates an array by calling a specific function on each element in the original array.
var numbers = [4, 9, 16, 25]; var x = numbers.map(Math.sqrt); document.write(x);
Result of the above example is:
Output // return 2,3,4,5
The javascript array reduce() method reduces the array to a single value.
var numArr = [ { name: 'a', num: 50}, { name: 'b', num: 50}, { name: 'c', num: 75}, { name: 'd', num: 35}, { name: 'e', num: 25 }, { name: 'f', num: 40 }, ]; var sum = numArr.reduce(function (total, currentValue) { return total + currentValue.num; }, 0); document.write( "javascript- Sum of the array value is :- " + sum );
Result of the above example is:
Output // return 275
The javascript array some() method checks whether at least one element of the array matches the given predicate. if none of the array elements match the predicate, then it will return false
var nums = [3, 18, 19, 20, 25]; function checkNumber(num) { return num >= 25; } document.write(nums.some(checkNumber));
Result of the above example is:
Output // return True
The javascript array every() method checks whether all elements of the array match the predicate:
var nums = [3, 18, 19, 20, 25]; function checkNumber(num) { return num >= 3; } document.write(nums.every(checkNumber));
Result of the above example is:
// return true
The javascript findIndex()
method is used to find first occurrence position from an array.
let numbers = [1, 2, 3, 4, 5]; let res = numbers.findIndex(e => e % 2 == 1); document.write(res); //
Result of the above example is:
// return 0
The js includes()
method returns true
if an javascript array contains a given element; Otherwise, it returns false
.
[1,2,3].includes(2); // true [1,2,3].includes(4); // false [1,2,3].includes(1,1); // false
Result of the above example is:
// return true false false
The js array indexOf()
method is used to find the first occurrence position of an array And returns the index of the first occurrence of the element. If the element is not found, -1 return.
var arr = [10, 20, 30, 40, 50, 70, 10, 50]; document.write(arr.indexOf(10)); // 0 document.write(arr.indexOf(10)); // 4 document.write(arr.indexOf(10)); // 5
Result of the above example is:
// return 0 4 5
This is similar to the indexOf() method, but one difference in these. The Javascript array lastIndexOf() method is used to find the last occurrence position of an array. It returns -1 if it cannot find the element.
var arr = [10, 20, 30, 40, 50, 70, 10, 40]; document.write(arr.lastIndexOf(10)); // 6 document.write(arr.lastIndexOf(40)); // 7
Result of the above example is:
// return 6 7
The javascript array isArray() method is used to check whether an object is an array. This function returns true if the object is an array, otherwise return false.
var arr = [5, 3, 10, 1, 6, 12] document.write("Retrun value = " + Array.isArray(arr));
Result of the above example is:
Retrun value = true