Reading Elements with Bracket Notation
You'll learn how to retrieve individual values from an array using their position number.
Now that you understand arrays use zero-based indexing, let's actually pull values out of an array. You do this with bracket notation — the same square brackets you used to create the array, but this time with an index number inside.
How Bracket Notation Works
Think of an array like a row of numbered mailboxes. To retrieve what's inside mailbox 0, you write the array's name followed by [0]. For mailbox 2, you write [2], and so on.
const fruits = ['apple', 'banana', 'cherry'];
const firstFruit = fruits[0]; // 'apple'
const secondFruit = fruits[1]; // 'banana'
const thirdFruit = fruits[2]; // 'cherry'
The number inside the brackets is the index — it tells JavaScript which position you want to read.
What You Get Back
When you use bracket notation, JavaScript returns the value stored at that position. You can assign it to a variable, use it in an expression, or pass it to a function — it's just a regular value.
const colors = ['red', 'green', 'blue'];
const favoriteColor = colors[1]; // 'green'
Invalid Indices
If you request an index that doesn't exist (like colors[10] in a 3-element array), JavaScript returns undefined — it doesn't throw an error.
const result = colors[99]; // undefined
Key Takeaway: Use bracket notation with an index number (arrayName[index]) to retrieve the value stored at that specific position in an array.