Everything, Everywhere
Verified Specification | Standardized Formulas | Instant Precision
Secure & Private (Zero Data Retention) Free Access • No Sign-Up

Arrays

Arrays are ordered collections of values. You can store strings, numbers, objects, or even other arrays inside them.

Try It Yourself

      
📋 Copy Modern JavaScript Array Methods Snippet
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)];

⚠️ 5 Fatal Traps & JavaScript Pitfalls

Trap #1: Array.prototype.sort() Converts to Strings by Default
Calling [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.
Trap #2: Mutating Methods (push, splice, reverse) Altering State
Legacy methods like sort(), reverse(), and splice() mutate the original array in-place, causing bugs in state-driven UI libraries. Use ES2023 immutable methods: toSorted(), toReversed(), toSpliced().
Trap #3: Sparse Arrays Created by new Array(length)
Writing 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 }).
Trap #4: delete arr[index] Leaves Empty Holes
Calling delete arr[1] deletes the value but leaves an empty slot without updating arr.length. Use splice() or toSpliced().
Trap #5: Shallow Array Cloning with Nested Objects
[...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.

💬 Frequently Asked Questions

What are the ES2023 non-mutating array methods?
ES2023 introduced toSorted(), toReversed(), toSpliced(), and with(), which perform sorting, reversing, splicing, and item replacement while returning a new array without mutating the original.
How do you check if a variable is an array in JavaScript?
Use Array.isArray(val). Never use typeof, which returns "object".
What is the fastest way to empty an array in JavaScript?
Setting array.length = 0 empties the array in-place and clears all elements for all references to that array.
What does the Array.prototype.flat() method do?
flat(depth) creates a new array with all sub-array elements concatenated recursively up to the specified depth (defaults to 1).
How does Array.from() create arrays from iterables or array-like objects?
Array.from(iterable, mapFn) converts array-like objects (such as NodeList or arguments) and iterables into genuine arrays, with an optional mapping transform.
Sponsored Utility
While You're Here
Sponsored Recommendations
Advertisement