new Array

Creates a new array with optional fixed length or initial elements.

// Empty array new Array(); // [] // Array with fixed length new Array(5); // [ <5 empty items> ] // Array with elements new Array("a", "b"); // ["a", "b"] // Array with array elements new Array([1, 2], [3]); // [[1, 2], [3]]

Array.from

Creates a new array instance from an array-like or iterable object.

// From array-like object Array.from("yo"); // ["y", "o"] // With map function Array.from( [1, 2], x => x * 2 ); // [2, 4] // Create an array of 3 zeroes Array.from( { length: 3 }, () => 0 ); // [0, 0, 0] // Convert Set to Array Array.from(new Set([1, 2, 2])); // [1, 2]

Array.of

Creates a new array from the provided arguments, regardless of type.

// Array with elements Array.of("a", "b"); // ["a", "b"] // Array with undefined element Array.of(undefined); // [undefined] // Array with a single number Array.of(3); // [3] // Array with nested arrays Array.of([1, 2], [3, 4]); // [[1, 2], [3, 4]]

for

A standard loop that iterates through a block of code a set number of times.

for (let i = 0; i < 3; i++) { console.log(i); } // 0, 1, 2
  • break
  • continue
  • throw

forEach

Executes a provided function once for each array element.

["a", "b"].forEach((val, idx) => console.log(val) ); // "a", "b"
  • return
  • throw

while

Repeats code while condition is true.

let i = 0; while (i < 3) { console.log(i); // 0, 1, 2 i++; }
  • break
  • continue
  • throw

do...while

Runs once, then repeats while true.

let i = 0; do { console.log(i); // 0, 1, 2 i++; } while (i < 3);
  • break
  • continue
  • throw

for...of

Loops through each value in an array, string, Map, or Set.

// Loop through array values const arr = ["a", "b"]; for (const value of arr) { console.log(value); } // "a", "b" // Loop through Set values const set = new Set([1, 2, 3]); for (const num of set) { console.log(num); } // 1, 2, 3
  • break
  • continue
  • throw

for...in

Loops through enumerable property names of an object.

// Basic object property iteration const obj = { a: 1, b: 2 }; for (const key in obj) { console.log(key); } // "a", "b" // Array indices as strings const arr = ["a", "b", "c"]; for (const index in arr) { console.log(index); } // "0", "1", "2"
  • break
  • continue
  • throw

at

Returns an element at a given index.

// Get element at index 1 [10, 20, 30].at(1); // 20 // Get element from the end [10, 20, 30].at(-1); // 30

concat

Merges arrays into a new array.

// Concatenate arrays [1, 2].concat([3, 4]); // [1, 2, 3, 4] // Concatenate with a single element ["a"].concat("b", "c"); // ["a", "b", "c"]

copyWithin

Copies part of an array to another location within it.

// Copy last two elements to the start [1, 2, 3].copyWithin(0, 2); // [3, 2, 3] // Copy elements within the middle [1, 2, 3].copyWithin(1, 0, 1); // [1, 1, 3]

find

Returns the first element passing the test function.

// Find first element starting with "c" ["apple", "banana", "cherry"].find( (val, idx) => val.startsWith("c") ); // "cherry"

findIndex

Returns the index of the first matching element.

// Find first index starting with "b" ["apple", "banana", "berry"].findIndex( (val, idx) => val.startsWith("b") ); // 1

findLast

Returns the last element passing the test function.

// Find last element starting with "b" ["apple", "banana", "berry"].findLast( (val, idx) => val.startsWith("b") ); // "berry"

indexOf

Returns first index of element, -1 if not found.

// Find the first occurrence of "b" ["a", "b", "c", "b"].indexOf("b"); // 1 // ...Start from index 2 ["a", "b", "c", "b"].indexOf("b", 2); // 3

join()

Joins array elements into a string.

// Join array elements with a dash separator ["a", "b", "c"].join("-"); // "a-b-c"

keys

Returns an iterator with array indices as keys.

let iterator = ["a", "b", "c"].keys(); iterator.next().value; // 0

reduce

Combines array elements into a single value.

// Sum all numbers [3, 4].reduce((a, v, idx) => a + v); // 7 // Join with prefix (initial value) [1, 2].reduce((a, v) => a + v, "x"); // "x12"

reduceRight

Combines array elements from right to left.

// Build array from right to left [1, 2, 3].reduceRight( (acc, val, idx) => [...acc, val], []); // [3, 2, 1]

reverse

Reverses array elements in place, modifying the array.

// Reverse the array in place ["a", "b", "c"].reverse(); // ["c", "b", "a"]

splice

Modifies array and returns deleted elements.

// Remove index 1 and insert "x" let arr = ["a", "b", "c"]; arr.splice(1, 1, "x"); // ["b"] // Remove the last two elements arr.splice(-2, 2); // ["x", "c"]

unshift

Adds elements to array start and returns new length.

let arr = ["b", "c"]; arr.unshift("a"); // 3 // arr is now ["a", "b", "c"]

values

Returns an iterator with values for each array index.

let iterator = ["a", "b", "c"].values(); iterator.next().value; // "a"

Square

Multiplies a number by itself to calculate its square.

// Using exponentiation operator 4 ** 2; // 16 // Using Math.pow() Math.pow(4, 2); // 16

% (Modulus)

Returns the remainder after dividing one number by another.

// Check if number is odd or even 5 % 2; // 1 (odd) 4 % 2; // 0 (even)

Math.pow

Raises a number to a specified power, equivalent to multiplying the base by itself for the given exponent.

// Raise 2 to the power of 3 Math.pow(2, 3); // 8

Math.random

Generates a random number between 0 and 1.

// Get a random decimal between 0 and 1 Math.random(); // 0.58974312 // Get a random number between 0 and 100 Math.floor(Math.random() * 101); // 42

entries

Returns an iterator containing index-value pairs for each array element.

let iterator = ["a", "b", "c"].entries(); iterator.next().value; // [0, "a"] iterator.next().value; // [1, "b"]

every

Checks if all elements pass the provided test function.

// Check if all numbers are positive [1, 2, 3].every((x, idx) => x > 0); // true // Check if all strings have length > 1 ["fe", "f"].every(x => x.length > 1); // false

fill

Fills array elements with a static value between indices.

// Fill middle section with a value [1, 2, 3].fill("x", 1, 2); // [1, "x", 3] // Fill entire array with a value [1, 2, 3].fill(0); // [0, 0, 0]

filter

Creates a new array with elements that pass the test function.

["apple", "banana", "berry"].filter( (val, idx) => val.startsWith("b") ); // ["banana", "berry"]

findLastIndex

Returns the index of the last element that satisfies the test function.

["apple", "banana", "berry"].findLastIndex( (val, idx) => val.startsWith("b") ); // 2

flat

Flattens nested arrays into a single array.

// Flatten array of arrays to depth 1 (default) [[1, 2], [3, 4]].flat(); // [1, 2, 3, 4] // Flatten nested arrays to depth 2 [1, [2, [3]]].flat(2); // [1, 2, 3]

flatMap

Maps and flattens each element into a new array.

// Map each element to an array and flatten ["a", "b"].flatMap( (val, idx) => [val, val] ); // ["a", "a", "b", "b"]

includes

Determines whether an array includes a certain element.

// Check if array includes element ["a", "b", "c"].includes("b"); // true // Check if array includes element starting from index 2 ["a", "b", "c", "b"].includes("b", 2); // true

lastIndexOf

Returns the last index of a value or -1 if not found.

// Search from start ["a", "b", "c", "b"].lastIndexOf("b"); // 3 // Search from index 2 ["a", "b", "c", "b"].lastIndexOf("b", 2); // 1

map

Creates a new array by transforming each element using the provided function.

["a", "b"].map( (val, idx) => val.toUpperCase() ); // ["A", "B"]

pop

Removes and returns the last element of the array. Modifies array.

let arr = ["a", "b", "c"]; arr.pop(); // "c"

push

Adds elements to end of array and returns new length. Modifies array.

// Push multiple elements at once let arr = ["x"]; arr.push("y", "z"); // 3 // Push arrays (adds as nested arrays) arr.push([2, 3]); // 4

shift

Removes and returns the first element of the array. Modifies array.

// Remove first element and store it let arr = ["a", "b", "c"]; arr.shift(); // "a" // arr is ["b", "c"]

slice

Creates a shallow copy of a portion of an array.

// Slice from index 1 to 3 ["a", "b", "c"].slice(1, 3); // ["b", "c"] // Slice the last two elements ["a", "b", "c"].slice(-2); // ["b", "c"]

some

Checks if at least one element passes the provided test function.

// Check if any string starts with "b" ["apple", "banana", "berry"].some( (val, idx) => val.startsWith("b") ); // true

sort

Sorts array elements in place.

// Sort alphabetically ["c", "a", "b"].sort(); // ["a", "b", "c"] // Sort numbers in ascending order [1, 11, 5].sort((a, b) => a - b); // [1, 5, 11]

toReversed

Returns a new array with elements reversed. Doesn't modify original array.

// Create reversed copy of array let arr = ["a", "b", "c"]; arr.toReversed(); // ["c", "b", "a"] // arr is still ["a", "b", "c"]

toSorted

Returns a new sorted array. Doesn't modify original array.

// Create sorted copy of array let arr = ["c", "a", "b"]; arr.toSorted(); // ["a", "b", "c"] // arr is still ["c", "a", "b"]

toSpliced

Returns new array with elements added/removed. Doesn't modify original array.

// Create copy with elements removed/added let arr = ["a", "b", "c"]; arr.toSpliced(1, 1, "x"); // ["a", "x", "c"] // arr is still ["a", "b", "c"]

with

Returns new array with element at index replaced. Doesn't modify original array.

// Create copy with element replaced let arr = ["a", "b", "c"]; arr.with(1, "x"); // ["a", "x", "c"] // arr is still ["a", "b", "c"]

Math.abs

Returns the absolute value of a number, making negative numbers positive.

// Get absolute value of a negative number Math.abs(-5); // 5 // Get absolute value of an expression Math.abs(-2 * 4); // 8

Math.sign

Returns the sign of a number: -1 for negative, 0 for zero, 1 for positive.

// Get sign of numbers Math.sign(-5); // -1 Math.sign(0); // 0 Math.sign(5); // 1

Math.sqrt

Returns the square root of a number.

// Get square root of a number Math.sqrt(16); // 4 Math.sqrt(2); // 1.4142135623730951

Math.trunc

Removes decimal digits, returning only the integer part.

// Remove decimal digits Math.trunc(3.7); // 3 Math.trunc(-3.7); // -3