Skip to content

JavaScript Array API

JavaScript Array methods for transforming, searching and mutating arrays.

1 class · 15 methods

Array

15 methods

Ordered list of values, zero-indexed, dynamically sized.

arr.map(callback, thisArg?)

Return a new array with the result of calling callback on every element.

Parameters

NameTypeDescription
callback(value, index, array) => anyFunction producing the new element.
thisArganyValue to use as `this` in callback.

Returns

Array

Example

javascript
[1, 2, 3].map(x => x * 2)        // [2, 4, 6]
['a', 'b'].map((v, i) => `${i}:${v}`)  // ['0:a', '1:b']
arr.filter(callback, thisArg?)

Return a new array with all elements for which callback returns truthy.

Parameters

NameTypeDescription
callback(value, index, array) => booleanPredicate function.
thisArganyValue to use as `this` in callback.

Returns

Array

Example

javascript
[1, 2, 3, 4].filter(x => x % 2 === 0)  // [2, 4]
['', 'a', null].filter(Boolean)        // ['a']
arr.reduce(callback, initialValue?)

Apply callback against an accumulator and each element to reduce to a single value.

Parameters

NameTypeDescription
callback(acc, value, index, array) => accReducer function.
initialValueanyInitial accumulator value.

Returns

any

Example

javascript
[1, 2, 3, 4].reduce((a, b) => a + b, 0)  // 10
['a', 'b', 'c'].reduce((acc, v) => ({...acc, [v]: true}), {})  // {a:true, b:true, c:true}
arr.forEach(callback, thisArg?)

Execute callback once for each element. Returns undefined.

Parameters

NameTypeDescription
callback(value, index, array) => voidFunction to execute per element.
thisArganyValue to use as `this` in callback.

Returns

undefined

Example

javascript
[1, 2, 3].forEach(x => console.log(x))  // logs 1, 2, 3
arr.find(callback, thisArg?)

Return the first element for which callback returns truthy, or undefined.

Parameters

NameTypeDescription
callback(value, index, array) => booleanPredicate function.
thisArganyValue to use as `this` in callback.

Returns

any

Example

javascript
[1, 2, 3, 4].find(x => x > 2)  // 3
[{id: 1}, {id: 2}].find(o => o.id === 2)  // {id: 2}
arr.some(callback, thisArg?)

Return true if at least one element satisfies the predicate.

Parameters

NameTypeDescription
callback(value, index, array) => booleanPredicate function.
thisArganyValue to use as `this` in callback.

Returns

boolean

Example

javascript
[1, 2, 3].some(x => x > 2)   // true
[1, 2, 3].some(x => x > 5)   // false
arr.every(callback, thisArg?)

Return true if all elements satisfy the predicate.

Parameters

NameTypeDescription
callback(value, index, array) => booleanPredicate function.
thisArganyValue to use as `this` in callback.

Returns

boolean

Example

javascript
[2, 4, 6].every(x => x % 2 === 0)  // true
[2, 4, 5].every(x => x % 2 === 0)  // false
arr.includes(searchElement, fromIndex?)

Return true if the array contains searchElement.

Parameters

NameTypeDescription
searchElementanyValue to find.
fromIndexnumberStart index (default 0).

Returns

boolean

Example

javascript
[1, 2, 3].includes(2)        // true
[1, 2, 3].includes(2, 2)     // false
['a', 'b'].includes('a')     // true
arr.indexOf(searchElement, fromIndex?)

Return the first index of searchElement, or -1 if not found.

Parameters

NameTypeDescription
searchElementanyValue to find.
fromIndexnumberStart index (default 0).

Returns

number

Example

javascript
[1, 2, 1].indexOf(1)      // 0
[1, 2, 1].indexOf(1, 1)   // 2
[1, 2].indexOf(3)         // -1
arr.slice(start?, end?)

Return a shallow copy of a portion of the array from start to end (end excluded).

Parameters

NameTypeDescription
startnumberStart index (default 0).
endnumberEnd index (default length).

Returns

Array

Example

javascript
[1, 2, 3, 4].slice(1, 3)  // [2, 3]
[1, 2, 3, 4].slice(-2)    // [3, 4]
[1, 2, 3].slice()         // [1, 2, 3] (shallow copy)
arr.splice(start, deleteCount?, ...items)

Remove, replace or insert elements in place. Returns the removed elements.

Parameters

NameTypeDescription
startnumberStart index.
deleteCountnumberNumber of elements to remove.
itemsanyItems to insert at start.

Returns

Array

Example

javascript
let a = [1, 2, 3];
a.splice(1, 1)        // [2], a == [1, 3]
a.splice(1, 0, 'x')   // [], a == [1, 'x', 3]
arr.concat(...values)

Return a new array that is the concatenation of the array with values.

Parameters

NameTypeDescription
values...anyArrays or values to concatenate.

Returns

Array

Example

javascript
[1, 2].concat([3, 4])    // [1, 2, 3, 4]
[1, 2].concat(3, [4])    // [1, 2, 3, 4]
arr.sort(compareFn?)

Sort the array in place. Without compareFn, elements are sorted as strings by UTF-16 code unit.

Parameters

NameTypeDescription
compareFn(a, b) => numberComparator returning negative, 0 or positive.

Returns

Array

Example

javascript
[3, 1, 2].sort()                       // [1, 2, 3]
[3, 1, 2].sort((a, b) => b - a)        // [3, 2, 1]
[10, 1, 2].sort()                      // [1, 10, 2] (string sort!)
arr.reverse()

Reverse the array in place.

Returns

Array

Example

javascript
[1, 2, 3].reverse()  // [3, 2, 1]
arr.flat(depth?)

Return a new array with sub-arrays concatenated up to depth (default 1).

Parameters

NameTypeDescription
depthnumberNested depth to flatten (default 1).

Returns

Array

Example

javascript
[1, [2, [3, [4]]]].flat()      // [1, 2, [3, [4]]]
[1, [2, [3, [4]]]].flat(2)     // [1, 2, 3, [4]]
[1, [2, [3, [4]]]].flat(Infinity)  // [1, 2, 3, 4]