Arrays
Arrays are ordered collections of values. You can store strings, numbers, objects, or even other arrays inside them.
Arrays are ordered collections of values. You can store strings, numbers, objects, or even other arrays inside them.
const numbers = [10, 5, 25, 40, 1];
// Immutable Modern Array Methods (ES2023+)
const sorted = numbers.toSorted((a, b) => a - b);
const reversed = numbers.toReversed();
const replaced = numbers.with(0, 99);
// Deduplication
const unique = [...new Set(numbers)];
[10, 5, 25, 1].sort() produces [1, 10, 25, 5] because JavaScript converts numbers to strings and sorts lexicographically. Always pass a comparator: (a, b) => a - b.sort(), reverse(), and splice() mutate the original array in-place, causing bugs in state-driven UI libraries. Use ES2023 immutable methods: toSorted(), toReversed(), toSpliced().new Array(5) creates empty slots, NOT undefined values. Iterative methods like map(), forEach(), and filter() completely skip empty slots! Use Array.from({ length: 5 }).delete arr[1] deletes the value but leaves an empty slot without updating arr.length. Use splice() or toSpliced().[...arr] and arr.slice() copy object references. Modifying an object inside the cloned array mutates the object in the original array. Use structuredClone() for deep copying.