Array Basics
👨💼 We need to store and access multiple values. Arrays let us keep an ordered
list of items.
Creating an array:
const fruits = ['apple', 'banana', 'cherry']
Accessing elements (zero-indexed):
console.log(fruits[0]) // "apple" (first element)
console.log(fruits[1]) // "banana" (second element)
console.log(fruits[2]) // "cherry" (third element)
- Create an array
colorswith at least 3 color names - Log the first element
- Log the last element (hint: use
colors.length - 1) - Change the second element to a different color
- Log the entire array
💰 You can modify array elements:
fruits[1] = 'blueberry' // Changes "banana" to "blueberry"