The every() method verifies whether all array values satisfy a condition.
For instance, this example checks if all array values are greater than 18:
Example
const numbers = [45, 4, 9, 16, 25]; let allOver18 = numbers.every(myFunction); function myFunction(value, index, array) { return value > 18; } |
Note that the callback function accepts 3 arguments:
When the callback function only uses the first parameter (value), the others can be omitted:
Example
const numbers = [45, 4, 9, 16, 25]; let allOver18 = numbers.every(myFunction); function myFunction(value) { return value > 18; } |
The some() method verifies if at least one array value passes a test.
In this example, it checks whether some array values are greater than 18:
Example
const numbers = [45, 4, 9, 16, 25]; let someOver18 = numbers.some(myFunction); function myFunction(value, index, array) { return value > 18; } |
The function accepts three arguments:
The Array.from() method generates an Array object from any object containing a length property or any iterable object.
Example
Generate an Array from a String.
Array.from(“ABCDEFG”); |
The from() method is a part of ES6 (JavaScript 2015), which has been fully supported in all modern browsers since June 2017.
The from() method is not supported in Internet Explorer.
The Array.keys() method generates an Array Iterator object containing the keys of the array.
Example
Generate an Array Iterator object containing the keys of the array.
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; const keys = fruits.keys(); for (let x of keys) { text += x + “<br>”; } |
keys() is fully supported in all modern browsers since June 2017 as it’s part of ES6.
The keys() method isn’t supported in Internet Explorer.
Example
Generate an Array Iterator, and then iterate through the key/value pairs.
const fruits = [“Banana”, “Orange”, “Apple”, “Mango”]; const f = fruits.entries(); for (let x of f) { document.getElementById(“demo”).innerHTML += x; } |
The entries() method yields an Array Iterator object containing key/value pairs:
[0, “Banana”]
[1, “Orange”]
[2, “Apple”]
[3, “Mango”]
It preserves the original array.
entries() is supported in all modern browsers since June 2017, as it’s part of ES6.
entries() is not available in Internet Explorer.
ES2023 introduced the Array with() method as a safe means of updating elements in an array without modifying the original array.
Example
const months = [“Januar”, “Februar”, “Mar”, “April”]; const myMonths = months.with(2, “March”); |
The spread (…) operator expands an iterable, such as an array, into multiple elements.
Example
const q1 = [“Jan”, “Feb”, “Mar”]; const q2 = [“Apr”, “May”, “Jun”]; const q3 = [“Jul”, “Aug”, “Sep”]; const q4 = [“Oct”, “Nov”, “May”]; const year = [...q1, ...q2, ...q3, ...q4]; |
The spread (…) operator is an ES6 feature introduced in JavaScript 2015. It’s fully supported in all modern browsers since June 2017.
The spread (…) operator is not supported in Internet Explorer.