Arrays are like containers that hold multiple related values in a specific order. Instead of creating separate variables for each item, you can store them all in one organized list.
Think of an array like a shopping list or a playlist—multiple items kept together in sequence.
Engagement Message
What's one real-world example where you naturally group related items together?
Creating an array in JavaScript is straightforward. You use square brackets []
and separate items with commas:
let fruits = ["apple", "banana", "orange"];
This creates an array called fruits
containing three string values in order.
Engagement Message
Which symbol here tells JavaScript you're creating an array?
Arrays can hold different types of data—numbers, strings, booleans—or even mix them together:
let gameStats = [150, "player1", true, 42];
This flexibility makes arrays perfect for storing various related information about the same topic.
Engagement Message
What mix of data types might you store for a user profile?
To access individual items in an array, you use their position number, which is called an index. The index goes inside square brackets:
let colors = ["red", "green", "blue"];
colors[0]
gives you "red".
colors[1]
gives you "green".
