Hello! Today, we will delve into JavaScript's data structures — Array
and Object
. Think of Arrays like a grocery list. In contrast, you can liken Objects to detailed recipes — each ingredient, (key), is paired with a quantity, (value). Let's explore!
Picture arrays as locker boxes, each holding an item. We declare an array in JavaScript as follows:
We access array items by an index:
Arrays in JavaScript are zero-indexed, which means the first element of an array is at index 0, the second element at index 1, and so on. To access values from an array, you simply refer to the item's position using its index:
In this example, fruits[0]
accesses the first element of the fruits
array, which is "Apple". Remember, the counting starts from 0, not 1. So, fruits[0]
is the first element, fruits[1]
is the second element, and so on. Isn't that easy?
