syntax Copy
const arr = [a, b, c];
const arr = Array.from (iterable);
const arr = Array.of (elements);example Copy
const fruits = ["apple" , "banana" , "cherry" ];
const range = Array.from ({ length: 5 }, (_, i) => i + 1 );
console.log(range);
const filled = new Array(3 ).fill(0 );
console.log(filled); Note Array(5) creates 5 empty slots, not [5]. Use Array.of(5) for a single-element array. Be careful with fill() on objects -- all slots share the same reference.
create array new array Array.from generate array fill array
syntax Copy
arr[index]
arr.at(index)example Copy
const colors = ["red" , "green" , "blue" , "yellow" ];
console.log(colors[0 ]);
console.log(colors.at(-1 ));
console.log(colors.at(-2 )); Note at() supports negative indexing. Bracket notation with negatives (arr[-1]) returns undefined because it looks for a property named "-1".
access array element get last element array at array index
Adding and Removing Elements syntax Copy
arr.push(item)
arr.pop()
arr.unshift(item)
arr.shift()
arr.splice(index, deleteCount, ...items)example Copy
const tasks = ["code" , "test" ];
tasks.push("deploy" );
tasks.unshift("plan" );
const removed = tasks.pop();
tasks.splice(2 , 0 , "review" );
console.log(tasks); Note push/pop are O(1). unshift/shift are O(n) because all indices must be renumbered. splice() mutates the original array.
add to array remove from array push pop splice insert into array
syntax Copy
arr.map(callback(element, index, array))example Copy
const prices = [10 , 25 , 50 ];
const withTax = prices.map(p => +(p * 1.08 ).toFixed(2 ));
console.log(withTax);output
[10.8, 27, 54]Note map() returns a new array of the same length. If you do not need the return value, use forEach() instead.
transform array map array apply function to each convert array elements
syntax Copy
arr.filter(callback(element, index, array))example Copy
const products = [
{ name: "Shirt" , price: 25 },
{ name: "Jacket" , price: 120 },
{ name: "Cap" , price: 15 },
];
const affordable = products.filter(p => p.price < 50 );
console.log(affordable.map(p => p.name));output
["Shirt", "Cap"]Note Returns a new array with elements that pass the test. The callback must return a truthy/falsy value.
filter array select elements remove from array by condition array where
syntax Copy
arr.reduce(callback(accumulator, current, index, array), initialValue)example Copy
const items = [
{ name: "Book" , price: 12 },
{ name: "Pen" , price: 3 },
{ name: "Bag" , price: 45 },
];
const total = items.reduce((sum, item) => sum + item.price, 0 );
console.log(total);output
60Note Always provide an initial value (second argument). Without it, reduce uses the first element and can throw on empty arrays.
reduce array sum array accumulate aggregate array fold
syntax Copy
arr.find(callback)
arr.findIndex(callback)
arr.findLast(callback)
arr.findLastIndex(callback)example Copy
const users = [
{ id: 1 , name: "Ava" },
{ id: 2 , name: "Bo" },
{ id: 3 , name: "Ava" },
];
console.log(users.find(u => u.name === "Ava" ));
console.log(users.findLast(u => u.name === "Ava" ));
console.log(users.findIndex(u => u.id === 2 )); Note find() returns the first match or undefined. findLast() (ES2023) searches from the end. Both return the element, not a boolean.
find in array search array findIndex findLast locate element
syntax Copy
arr.sort(compareFn)
arr.toSorted(compareFn)example Copy
const nums = [40 , 1 , 5 , 200 ];
console.log([...nums].sort());
console.log([...nums].sort((a, b) => a - b));
const sorted = nums.toSorted((a, b) => b - a);
console.log(sorted);
console.log(nums); Note sort() MUTATES the array and defaults to string comparison. Always pass a compare function for numbers. Use toSorted() for a non-mutating alternative.
sort array array sort sort numbers toSorted non-mutating sort
syntax Copy
arr.flat(depth)
arr.flatMap(callback)example Copy
const nested = [[1 , 2 ], [3 , [4 , 5 ]]];
console.log(nested.flat());
console.log(nested.flat(2 ));
const sentences = ["hello world" , "good morning" ];
const words = sentences.flatMap(s => s.split(" " ));
console.log(words); Note flatMap() is equivalent to map() followed by flat(1) but more efficient. Use flat(Infinity) to flatten any depth.
flatten array flat flatMap nested array unwrap array
includes(), some(), every() syntax Copy
arr.includes(value)
arr.some(callback)
arr.every(callback)example Copy
const perms = ["read" , "write" , "delete" ];
console.log(perms.includes("write" ));
const ages = [22 , 17 , 30 , 15 ];
console.log(ages.some(a => a < 18 ));
console.log(ages.every(a => a >= 18 )); Note includes() uses strict equality (===) and works with NaN. some() short-circuits on the first true; every() short-circuits on the first false.
array contains check if in array includes some every any match
Non-Mutating Alternatives (ES2023) syntax Copy
arr.toSorted(fn)
arr.toReversed()
arr.toSpliced(start, deleteCount, ...items)
arr.with (index, value)example Copy
const original = [3 , 1 , 4 , 1 , 5 ];
const reversed = original.toReversed();
console.log(reversed);
const replaced = original.with (2 , 99 );
console.log(replaced);
console.log(original); Note These return new arrays, leaving the original unchanged. Ideal for immutable state patterns (React, Redux, etc).
immutable array toSorted toReversed toSpliced array with non-mutating
syntax Copy
structuredClone(value)example Copy
const original = [
{ name: "Ava" , scores: [90 , 85 ] },
{ name: "Leo" , scores: [78 , 92 ] },
];
const clone = structuredClone(original);
clone[0 ].scores.push(100 );
console.log(original[0 ].scores);
console.log(clone[0 ].scores); Note structuredClone handles nested objects, arrays, Maps, Sets, Dates, RegExps, and more. Does NOT clone functions, DOM nodes, or prototypes.
deep clone array copy nested array structuredClone deep copy