ā¤ Like
šŸ”– Save
šŸ”— Share
@thedevspaceio
@thedevspaceio

JavaScript Arrays Cheatsheet

@thedevspaceio

šŸ—‚ļø JavaScript Arrays Cheatsheet

Arrays are ordered collections of data that power most data handling in JavaScript.

This cheatsheet includes a complete reference of array properties and methods for creating, transforming, searching, iterating, ordering, joining, and flattening arrays.

āœ… Array properties and methods āœ… Creating arrays āœ… Adding and removing elements āœ… Transforming arrays āœ… Searching arrays āœ… Iterating arrays āœ… Ordering and joining āœ… Flattening arrays

#javascript #arrays #map #filter #reduce #spread #slice #find #sort #webdev


Array properties and methods

MethodMutatingDescription
Array.lengthReturns the number of elements in the array.
Array.isArray()Checks if a value is an array.
Array.from()Creates an array from an iterable or array-like object.
Array.of()Creates an array from the given arguments.
.push()YesAdds elements to the end.
.pop()YesRemoves the last element.
.unshift()YesAdds elements to the start.
.shift()YesRemoves the first element.
.concat()Merges arrays into a new array.
.slice()Returns a shallow copy of a portion.
.splice()YesAdds/removes elements.
.indexOf()Returns the first index of a value.
.lastIndexOf()Returns the last index of a value.
.includes()Checks if a value exists.
.find()Returns the first matching element.
.findIndex()Returns the index of the first matching element.
.findLast()Returns the last matching element.
.findLastIndex()Returns the index of the last matching element.
.filter()Returns a new array of matching elements.
.map()Returns a new array of transformed elements.
.reduce()Reduces the array to a single value.
.reduceRight()Reduces from right to left.
.every()Returns true if all elements match.
.some()Returns true if any element matches.
.forEach()Runs a function for each element.
.sort()YesSorts the array.
.reverse()YesReverses the array.
.join()Joins elements into a string.
.flat()Flattens nested arrays.
.flatMap()Maps then flattens one level.
.fill()YesFills elements with a value.
.at()Returns element at index, supports negatives.

Creating arrays

Array.isArray()

Checks if a value is an array.

js
Array.isArray([1, 2, 3]); // true
Array.isArray("hello"); // false

Array.from()

Creates an array from an iterable or array-like object.

js
Array.from("hello"); // ["h", "e", "l", "l", "o"]
Array.from({ length: 3 }, (_, i) => i); // [0, 1, 2]

Array.of()

Creates an array from the given arguments.

js
Array.of(1, 2, 3); // [1, 2, 3]
Array.of(7); // [7]

Adding and removing elements

.push() / .pop()

Add or remove elements at the end. Both mutate the array.

js
const arr = [1, 2, 3];
arr.push(4); // [1, 2, 3, 4]
arr.pop(); // 4

.unshift() / .shift()

Add or remove elements at the start. Both mutate the array.

js
const arr = [2, 3];
arr.unshift(1); // [1, 2, 3]
arr.shift(); // 1

.splice()

Adds or removes elements at a specific index. Mutates the array.

js
const arr = [1, 2, 3, 4];
arr.splice(1, 2); // removes 2 elements starting at index 1
// arr is now [1, 4]
 
arr.splice(1, 0, 2, 3); // inserts 2 and 3 at index 1
// arr is now [1, 2, 3, 4]

Transforming arrays

.map()

Returns a new array with each element transformed.

js
[1, 2, 3].map((x) => x * 2); // [2, 4, 6]

.filter()

Returns a new array of elements that pass the test.

js
[1, 2, 3, 4].filter((x) => x % 2 === 0); // [2, 4]

.reduce()

Reduces the array to a single value.

js
[1, 2, 3, 4].reduce((sum, value) => sum + value, 0); // 10

Searching arrays

.includes()

Checks if a value exists.

js
[1, 2, 3].includes(2); // true
[1, 2, 3].includes(5); // false

.find() / .findIndex()

Find the first matching element (.find()) or its index (.findIndex()).

js
[1, 2, 3].find((x) => x > 1); // 2
[1, 2, 3].findIndex((x) => x > 1); // 1

Iterating arrays

.forEach()

Runs a function for each element.

js
[1, 2, 3].forEach((x) => console.log(x));

.every()

Returns true if all elements pass the test.

js
[2, 4, 6].every((x) => x % 2 === 0); // true
[1, 2, 3].every((x) => x > 0); // true

.some()

Returns true if at least one element passes the test.

js
[1, 2, 3].some((x) => x > 2); // true
[1, 2, 3].some((x) => x > 5); // false

Ordering and joining

.sort()

Sorts the array in place. Mutates the array.

js
[3, 1, 2].sort((a, b) => a - b); // [1, 2, 3]

.reverse()

Reverses the array in place. Mutates the array.

js
[1, 2, 3].reverse(); // [3, 2, 1]

.join()

Joins elements into a string.

js
["a", "b", "c"].join("-"); // "a-b-c"

Flattening arrays

.flat()

Flattens nested arrays.

js
[1, [2, 3], [4, [5]]].flat(); // [1, 2, 3, 4, [5]]
[1, [2, [3]]].flat(2); // [1, 2, 3]

.flatMap()

Maps each element then flattens the result one level.

js
[1, 2, 3].flatMap((x) => [x, x * 2]); // [1, 2, 2, 4, 3, 6]

Full-Stack AI Developer Roadmap

From HTML & CSS to working with AI models, all in one structured roadmap.

@thedevspaceio
www.thedevspace.io