What Is an Array?
An array stores multiple values in a single variable. Arrays use square brackets [ ].
let cars = ["Audi", "BMW", "Subaru"];
console.log(cars);
Accessing Items
console.log(cars[0]); // Audi
console.log(cars[1]); // BMW
Modifying Array Items
cars[1] = "Mercedes";
Array Length
console.log(cars.length);
Adding Items
Push (adds to end)
cars.push("Toyota");
Unshift (adds to start)
cars.unshift("Volvo");
Removing Items
Pop (removes last)
cars.pop();
Shift (removes first)
cars.shift();
Looping Through an Array
Classic For Loop
for (let i = 0; i < cars.length; i++) {
console.log(cars[i]);
}
For…of Loop
for (let brand of cars) {
console.log(brand);
}
forEach
cars.forEach(car => {
console.log(car);
});
Joining Array Elements
let text = cars.join(", ");
console.log(text);
Checking If It's an Array
console.log(Array.isArray(cars)); // true
Nested Arrays
let matrix = [
[1, 2],
[3, 4]
];
console.log(matrix[1][0]); // 3
Why Arrays Matter
- Store large sets of data easily
- Simple to loop through
- Perfect for lists, tables, menus, grids, etc.
- Core part of JavaScript logic
What’s Next?
Next you’ll learn CSS animations and how to make your website feel alive and dynamic.