An Array is a special variable that can hold more than one value at a time. It’s like a chest of drawers where each drawer is numbered (starting from 0).
1. Zero-Based Indexing
The first item in an array is at index 0, the second is at 1, and so on.
2. Modern Array Methods
Instead of using loops for everything, modern JavaScript gives us powerful "built-in" tools:
.push(): Adds an item to the end..pop(): Removes the last item..map(): Creates a new array by doing something to every item..filter(): Creates a new array with only the items that pass a "test."
The Code Example
// 1. Creating an Array
const fastFood = ["Burger", "Pizza", "Taco"];
// 2. Accessing and Modifying
console.log(fastFood[0]); // "Burger"
fastFood[1] = "Pasta"; // Changes Pizza to Pasta
fastFood.push("Fries"); // Adds Fries to the end
// 3. The Powerhouse Methods (ES6)
const numbers = [10, 20, 30, 40, 50];
// .map() - Multiply every number by 2
const doubled = numbers.map(num => num * 2);
// doubled is [20, 40, 60, 80, 100]
// .filter() - Only keep numbers greater than 25
const bigNumbers = numbers.filter(num => num > 25);
// bigNumbers is [30, 40, 50]
console.log(doubled, bigNumbers);