All stacks / Intents / Array at Also written as array with · Array.from
Creating Arrays JS · Arrays 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.
Accessing Elements JS · Arrays 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".
Non-Mutating Alternatives (ES2023) JS · Arrays 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).
Array/String .at() Method JS · Modern Features syntax Copy
arr.at(index)
str.at(index)example Copy
const stack = ["first" , "second" , "third" , "last" ];
console.log(stack.at(0 ));
console.log(stack.at(-1 ));
console.log(stack.at(-2 ));
console.log("hello" .at(-1 )); Note Works on Arrays, Strings, and TypedArrays. The main advantage over bracket notation is support for negative indices.
Related tasks