This page is a scan list for day-to-day array work. For copying and combining collections with rest/spread, see Destructuring and spread .
Iterate
Avoid for..in on arrays—it walks enumerable keys, not just indices.
for ( const val of arr) console. log (val);
arr. forEach (( val ) => console. log (val));
Create
const literal = [ 1 , 2 , 3 ];
Array. from ( "foo" ); // ["f", "o", "o"]
Array. from ([ 1 , 2 , 3 ], ( x ) => x + x); // [2, 4, 6]
Array. from ({ length: 5 }, ( _ , i ) => i); // [0, 1, 2, 3, 4]
A literal [a, b, c] behaves like an object with numeric keys 0..length-1 and a length property. Array(n) creates a sparse array with length n and no index properties—map and friends skip missing indices.
Mutate ends and middle
let arr = [ 1 , 2 , 3 , 4 , 5 ];
arr. pop (); // 5 → [1, 2, 3, 4]
arr. shift (); // 1 → [2, 3, 4]
arr. push ( 6 ); // [2, 3, 4, 6]
arr. splice ( 1 , 2 , 9 ); // removed [3, 4] → [2, 9, 6]
Assigning arr[i] = x grows the array with empty slots: const a = []; a[2] = 3 → [empty × 2, 3].
Copy, concat, and slice
const copy = arr. slice ();
const merged = arr. concat ([ 4 , 5 ]);
Search and membership
Non-mutating unless noted.
const nums = [ 5 , 12 , 8 , 130 , 44 ];
nums. find (( n ) => n > 10 ); // 12
nums. findLast (( n ) => n > 10 ); // 44
Non-mutating updates (ES2023)
These return a new array and leave the original unchanged—useful when you hold shared references or React state.
const months = [ "March" , "Jan" , "Feb" , "Dec" ];
months. toSorted (); // ["Dec", "Feb", "Jan", "March"]
ids. with ( 1 , 99 ); // [1, 99, 3]
Caution
sort() and reverse() still mutate in place. For numeric sort, pass a comparator: (a, b) => a - b.
Shape and join
[ 1 , 2 , [ 3 , 4 ]]. flat (); // [1, 2, 3, 4]
[ "it's Sunny in" , "" , "California" ]. flatMap (( s ) => s. split ( " " ));
Higher-order methods
[ 1 , 4 , 9 ]. map (( x ) => x * 2 ); // [2, 8, 18]
[ "a" , "bb" , "ccc" ]. filter (( s ) => s. length > 1 ); // ["bb", "ccc"]
[ 1 , 2 , 3 , 4 ]. reduce (( acc , n ) => acc + n, 0 ); // 10
Sparse-array pitfall
Array ( 10 ). map (( _ , i ) => i); // array of 10 empty slots — callback never runs
Array. from ({ length: 10 }, ( _ , i ) => i); // [0..9]
Array. from ( Array ( 10 ). keys ()); // [0..9]
Continue with Set in JavaScript .