JavaScript Array Methods – In this tutorial we are going to show you the daily useful and important JavaScript Array Methods with examples. Javascript array methods or functions, to help write clean code with less effort.
In this tutorial, we will demonstrate daily useful 18 array methods of javascript with examples.
JavaScript: Array Methods
The basic daily useful array methods in javascript are the following:
- Array pop() Method
- Array push() Method
- Array toString() Method
- Array join() Method
- Array splice() Method
- Array sort() Method
- Array shift() Method
- Array unshift() Method
- Array reverse() Method
- Array concat() Method
- Array slice() Method
- Array filter() Method
- Array find() Method
- Array forEach() Method
- Array map() Method
- Array reduce() Method
- Array some() Method
- Array every() Method
1. Array pop() Method
The “pop()” method is used when you want to remove the last element of an array.
The method returns the removed items value.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.pop();
Result of the above example is:
Output
// New array: PHP, Python, Java
2. Array push() Method
The “push()” method is used to add a new element to the end of an array.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.push("JavaScript");
Result of the above example is:
Output
// New array: PHP, Python, Java, C, JavaScript
3. Array toString() Method
The “toString()” method is used to convert array to string.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.toString();
Result of the above example is:
// output
PHP, Python, Java, C
4. Array join() Method
The join() used to join the elements of an array into a string.
The “join()” method puts all the elements of the array into a string list. This method difference from “toString()” method.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.join(" - ");
Result of the above example is:
// output
PHP - Python - Java - C
5. Array splice() Method
The “splice()” method can add and remove items form an array.
syntax
================
array.splice(index, howMany, [element1][, …, elementN]);
- index − Index param specifies where a new item should be inserted.
- howMany − An integer indicating the number of old array elements to remove.
- If howMany set to 0, no items will be removed in array list.
Ex:-
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
6. Array sort() Method
This method either alphabetic or numeric sorts an array.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.sort();
Result of the above example is:
// Outuput
array: C, Java, PHP, Python
7. Array shift() Method
This method removes the first element of an array. like the “pop()” method, removes an element from an array.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.shift();
Result of the above example is:
// Output
new array: Python, Java, C
8. Array unshift() Method
The unshift() method adds a new element to the beginning of an array.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; cars.unshift("JavaScript");
Result of the above example is:
//Output
New array: JavaScript, PHP, Python, Java, C
9. Array reverse() Method
The method is used for reverses the order of the elements in an array.
Ex:-
var lang = ["PHP", "Python", "Java", "C"]; lang.reverse();
Result of the above example is:
// Outuput
array: C, Java, PHP, Python
10. Array concat() Method
The “concat()” method joins two or more arrays and makes a new one.
Ex:-
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
11. Array slice() Method
The method is used to selected elements in an array and makes a new one. It can take one or two arguments.
Ex:-
var lang = ["PHP", "Python", "Java", "C", "JavaScript"]; var lang = cars.slice(1, 4);
Result of the above example is:
//Output
New Array : Python, JavaScript
12. Array filter() Method
This filter method is used to filter the array, according our conditions.
Ex:-
<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.
13. Array find() Method
This find method is used find the first element of an array.
Ex:-
function isCheck(value) { return value >= 10; } var find= [10, 5, 16, 4, 7, 12].find(isCheck);
Result of the above example is:
Output
// 12
14. Array forEach() Method
The JavaScript Array Method – forEach () imposes a specific function for each item in a particular array individually:
Ex:-
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
15. Array map() Method
The map () method in JavaScript creates an array by calling a specific function on each element in the original array.
Ex:-
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
16. Array reduce() Method
The javascript array reduce() method reduces the array to a single value.
Ex:-
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
17. Array some() Method
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
Ex:-
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
18. Array every() Method
The javascript array every() method checks whether all elements of the array match the predicate:
Ex:-
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
You may like
- Convert String to Array JavaScript
- JavaScript Array splice() Method By Example
- Sorting Using JavaScript Array Sort() Method
- JavaScript: Clone an Array & Object By Examples
- Remove First, last, or specific Element From Array Javascript
- javaScript Push() Array By Example
- javaScript Push Array, Items Into Array Example
- Convert Array to Comma-Separated String javaScript
- JavaScript Check the Object | isArray() Function