π¦ JavaScript Array -
Imagine Riya has a big fruit box πππ.
Instead of keeping every fruit in a separate box, she puts all the fruits into one big box. This big box is like a JavaScript Array.
π Story
Riya creates a fruit box:
let fruits = ["Apple", "Banana", "Mango", "Orange"];Her box looks like this:
π FRUIT BOX
βββββββββββ¬ββββββββββ¬ββββββββββ¬ββββββββββ
β Apple β Banana β Mango β Orange β
βββββββββββ΄ββββββββββ΄ββββββββββ΄ββββββββββ
0 1 2 3
Index Index Index Indexπ Each item has a position called an index.
Get Raveenanagarβs stories inΒ yourΒ inbox
Join Medium for free to get updates fromΒ thisΒ writer.
Important: JavaScript arrays start counting from 0, not 1.
π Getting a Fruit
Riya wants the Mango:
console.log(fruits[2]);Output:
MangoBecause Mango is at index 2.
β Adding a Fruit
Riya buys Grapes and puts them in the box:
fruits.push("Grapes");Now:
ββββββββββ¬βββββββββ¬βββββββββ¬βββββββββ¬βββββββββ
β Apple β Banana β Mango β Orange β Grapes β
ββββββββββ΄βββββββββ΄βββββββββ΄βββββββββ΄βββββββββ
0 1 2 3 4ποΈ Removing a Fruit
She removes the last fruit:
fruits.pop();Grapes are removed from the end.
π§ Simple Definition
An array is a container that stores multiple values in a single variable.
let fruits = ["Apple", "Banana", "Mango"];Think of it as:
Array = π§Ί One basket β π₯ Many items
π Easy Formula
ARRAY
β
One container
β
Multiple values
β
Each value has an index
β
Index starts from 0








