Back

Arrays

An array is an ordered list of values, accessed by index starting at 0. Reading by index is fast (O(1)); searching for a value means checking items one by one (O(n)).

const nums = [10, 20, 30]

nums[0]            // 10
nums.length        // 3
nums.push(40)      // add to end
nums.pop()         // remove from end
nums.includes(20)  // true

const doubled = nums.map((n) => n * 2)       // [20, 40, 60]
const big = nums.filter((n) => n > 15)       // [20, 30]
const total = nums.reduce((a, b) => a + b, 0) // 60

Roadmap