All tutorials
Basics

Arrays

Ordered lists with powerful built-in methods.

1. Explanation

Arrays store ordered collections accessed by index (starting at 0). Useful methods: push/pop (end), shift/unshift (start), slice (copy a range), splice (mutate), map (transform), filter (keep matching), reduce (aggregate), find, includes, indexOf, sort, reverse. map/filter/reduce return new arrays without mutating the original.

2. Example

Try it yourself
Console
Click Run to see output.

3. Expected output

output
[2,4,6,8,10]
[2,4]
sum = 15
includes 3? true

4. Common mistakes

  • Mutating with splice when map/filter would be cleaner
  • Forgetting that sort() converts to strings by default — use (a,b)=>a-b for numbers
  • Reading arr[arr.length] expecting the last element — it's undefined; use arr[arr.length-1]

5. Practice questions

  1. Find the largest number in an array without using Math.max
  2. Remove duplicates from an array using new Set
  3. Reverse an array without using .reverse()
→ Try more practice problems