Arrays
EasyIn plain terms
An array is an ordered list of values. The first item is at index 0, the second at index 1, and so on (this is called "zero-indexed"). You can mix types in one array—numbers, strings, objects, even other arrays.
Arrays have built-in methods for common tasks: push and pop add/remove from the end; map creates a new array by transforming each item; filter keeps only items that pass a test; reduce combines all items into a single value. These methods make array work much easier than writing loops by hand.
What you need to know
- •Ordered, zero-indexed
- •map, filter, reduce
- •Reference type
Try it yourself
Copy the code below and run it in your browser console or a code editor:
const arr = [1, 2, 3];
arr.push(4); // [1,2,3,4]
arr.pop(); // [1,2,3]
arr.map(x => x*2); // [2,4,6]
arr.filter(x => x>1); // [2,3]
arr.reduce((a,b) => a+b, 0); // 6
arr.includes(2); // true
arr.indexOf(2); // 1
arr.slice(1,3); // [2,3]
arr.splice(1, 1); // remove 1 at index 1Learn more
Dive deeper with these trusted resources: