Skip to content
Arrays
step 1/7

Reading — step 1 of 7

Learn

~2 min readCollections

An array is an ordered list of values. JavaScript arrays are dynamic — they grow and shrink as you add and remove elements. A single array can hold mixed types (though in practice, keep types consistent for clarity).

Creating arrays

js

Mutating methods (modify in place)

js

Non-mutating methods (return a new array)

js

The spread operator ... is the modern way to copy/concat. Mutating methods are footguns when you accidentally share references — non-mutating methods avoid that.

Searching

js

Iterating

js

forEach doesn't return anything useful. For transformations, use map (next lesson).

Length is mutable!

js

Assigning to length resizes the array. Setting it to 0 empties it.

Common mistakes

  • Out-of-bounds access returns undefined silently — no exception. Always sanity-check.
  • Comparing arrays with === — checks reference identity, not contents. [1] === [1] is false.
  • Mutating sort() and reverse() when you wanted a new array. Use [...arr].sort() instead.
  • Default sort() sorts as STRINGS: [10, 2, 1].sort() is [1, 10, 2]. Pass a comparator: .sort((a, b) => a - b).

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…