Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JavaScript Array every

JavaScript Array every()

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 = [45491625];
let allOver18 = numbers.every(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

Note that the callback function accepts 3 arguments:

  • The value of the current array element.
  • The index of the current array element.
  • The array itself.

When the callback function only uses the first parameter (value), the others can be omitted:

Example

const numbers = [45491625];
let allOver18 = numbers.every(myFunction);

function myFunction(value) {
  return value > 18;
}

JavaScript Array some()

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 = [45491625];
let someOver18 = numbers.some(myFunction);

function myFunction(value, index, array) {
  return value > 18;
}

The function accepts three arguments:

  • The value of the item
  • The index of the item
  • The array itself

JavaScript Array.from()

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”);

Browser Support

The from() method is a part of ES6 (JavaScript 2015), which has been fully supported in all modern browsers since June 2017.

IMG_4152

The from() method is not supported in Internet Explorer.

JavaScript Array keys()

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>”;
}

Browser Support

keys() is fully supported in all modern browsers since June 2017 as it’s part of ES6.

IMG_4152

The keys() method isn’t supported in Internet Explorer.

JavaScript Array entries()

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.

Browser Support

entries() is supported in all modern browsers since June 2017, as it’s part of ES6.

IMG_4152

entries() is not available in Internet Explorer.

JavaScript Array with() Method 

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”);

JavaScript Array Spread (…)

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];

Browser Support

The spread () operator is an ES6 feature introduced in JavaScript 2015. It’s fully supported in all modern browsers since June 2017.

IMG_4152

The spread () operator is not supported in Internet Explorer.