An array is an ordered list of values, stored under a single variable name. Instead of creating fruit1, fruit2, fruit3, you create one array holding all of them.
let fruits = ["apple", "banana", "mango"];
console.log(fruits); // ["apple", "banana", "mango"]Each item has a position, called its index, starting from 0 — not 1.
let fruits = ["apple", "banana", "mango"];
console.log(fruits[0]); // "apple" — first item
console.log(fruits[1]); // "banana"
console.log(fruits[2]); // "mango" — last item| Method | What it does | Example |
|---|---|---|
.push(x) | Adds an item to the end | fruits.push("kiwi") |
.pop() | Removes the last item | fruits.pop() |
.length | Number of items | fruits.length → 3 |
.indexOf(x) | Finds the position of a value | fruits.indexOf("banana") → 1 |
let fruits = ["apple", "banana"];
fruits.push("mango");
console.log(fruits.length); // 3
console.log(fruits); // ["apple", "banana", "mango"]