Arrays
Prerequisites: for loops
An array is an ordered, changeable list of values.
Syntax
const fruits = ["apple", "banana", "cherry"];
Example
fruits.push("date");
console.log(fruits[0]);
console.log(fruits.length);
console.log(fruits.includes("banana"));
Expected output
apple
4
true
Indexing starts at 0. .push() adds to the end. Common array
methods .map(), .filter(), and .reduce() each take a callback
function and return a new array (or value) without changing the
original:
const doubled = [1, 2, 3].map((n) => n * 2); // [2, 4, 6]
Common mistakes
- Accessing an index that doesn't exist returns
undefinedrather than raising an error — checkarray.lengthfirst if you're not sure the index is valid.
Practice
Create an array of three favorite movies and log the second one.
Show solution
const movies = ["Inception", "Arrival", "Interstellar"];
console.log(movies[1]);