Learn how to store, access, and manipulate ordered collections of values — the most essential data structure in JavaScript for handling lists of any kind.
An array in JavaScript is a special type of object used to
store multiple values in a single variable, where each value is stored at a
specific index starting from 0. Arrays can hold elements
of any data type — numbers, strings, booleans, objects, or even other arrays.
Arrays are ordered, meaning the position of each element is meaningful and preserved. They come with a rich set of built-in methods for adding, removing, searching, and transforming their contents.
An array is like a numbered list — instead of creating a separate variable for each value, you put them all in one place:
["red", "blue", "green"]
0
length - 1
let fruits = ["apple", "banana", "mango"];
console.log(fruits); // full array
console.log(fruits[0]); // "apple" — first element
console.log(fruits[2]); // "mango" — last element
console.log(fruits.length); // 3
0 — the first element is fruits[0], the last is fruits[length - 1]length is always one more than the last valid indexundefined — no errorlet numbers = [1, 2, 3];
numbers.push(4); // add at END → [1, 2, 3, 4]
console.log(numbers);
numbers.pop(); // remove from END → [1, 2, 3]
console.log(numbers);
numbers.unshift(0); // add at START → [0, 1, 2, 3]
console.log(numbers);
numbers.shift(); // remove from START → [1, 2, 3]
console.log(numbers);
push() / pop() — add and remove at the endunshift() / shift() — add and remove at the startlet colors = ["red", "green", "blue"];
// Classic for loop
for (let i = 0; i < colors.length; i++) {
console.log(i + ": " + colors[i]);
}
// Modern for...of loop
for (let color of colors) {
console.log(color.toUpperCase());
}
for loop uses an index — useful when you need the positionfor...of gives each element directly — cleaner when you only need the valuearray.length as the loop boundary — never hardcode the countlet fruits = ["apple", "banana", "mango"];
console.log(fruits.includes("banana")); // true
console.log(fruits.includes("grape")); // false
console.log(fruits.indexOf("mango")); // 2
console.log(fruits.indexOf("grape")); // -1 (not found)
console.log(fruits.join(", ")); // "apple, banana, mango"
includes() → returns true/false — great for existence checksindexOf() → returns the position, or -1 if not foundjoin(separator) → converts the array to a single string with each element separated by the given characterlet matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(matrix[0][1]); // row 0, col 1 → 2
console.log(matrix[1][2]); // row 1, col 2 → 6
console.log(matrix[2][0]); // row 2, col 0 → 7
array[row][column]Think of an array like a shopping list — one place to hold all related items:
Click ▶ Preview, then press the button to render an array of
fruits as a live list on the page — using join() and a loop.
<!DOCTYPE html>
<html>
<head><title>Arrays Demo</title></head>
<body style="font-family:sans-serif;padding:24px;background:#f8f9fa">
<h2>Fruits List</h2>
<button onclick="showFruits()"
style="padding:8px 18px;background:#f7df1e;border:none;
border-radius:6px;cursor:pointer;font-weight:700;font-size:1rem">
Show Fruits
</button>
<div id="output" style="margin-top:16px;line-height:2;font-size:0.95rem"></div>
<script>
function showFruits() {
let fruits = ["Apple", "Banana", "Mango", "Grapes"];
let listHtml = "<b>Fruits array:</b> [" + fruits.join(", ") + "]<br><br>";
listHtml += "<b>Each fruit:</b><ul style='margin:6px 0 0 20px'>";
for (let i = 0; i < fruits.length; i++) {
listHtml += `<li>[${i}] ${fruits[i]}</li>`;
}
listHtml += "</ul>";
listHtml += `<br><b>Total items:</b> ${fruits.length}`;
document.getElementById("output").innerHTML = listHtml;
}
</script>
</body>
</html>
let colors = ["red", "green", "blue"];
0colors.length - 1colors.lengthlet nums = [10, 20, 30];
40 to the end using push() — print the arraypop() — print the array again3let arr = ["a", "b", "c"];
console.log(arr[0]);
0. The first element is always at index 0, the last at length - 1.
push() do?
let arr = [1, 2, 3];
arr.push(4);
console.log(arr);
push() appends one or more elements to the end of the array and returns the new length. To add at the start, use unshift() instead.
let arr = [1, 2, 3];
console.log(arr[1]);
1 refers to the second element — 2. Index 0 is 1, index 2 is 3.
Arrays can store mixed data types — numbers, strings, booleans, objects, or even other arrays all in one:
let data = [1, "Aman", true, null, { score: 95 }];
console.log(data[0]); // 1
console.log(data[1]); // "Aman"
console.log(data[4].score); // 95
Use the spread operator (...) to copy an array without mutation — never use arr2 = arr1 if you want an independent copy:
let original = [1, 2, 3];
let copy = [...original]; // independent copy
copy.push(4);
console.log(original); // [1, 2, 3] — unchanged
console.log(copy); // [1, 2, 3, 4]
Avoid assigning values to arbitrary high indices — it creates sparse arrays with empty slots that cause unexpected undefined values:
let arr = [1, 2, 3];
arr[9] = "value"; // creates slots 3–8 as empty
console.log(arr.length); // 10 — not 4!
console.log(arr[5]); // undefined — empty slot
Use Array.isArray() — not typeof — to correctly detect arrays in your validation logic:
let arr = [1, 2, 3];
let obj = { a: 1 };
console.log(typeof arr); // "object" — not helpful!
console.log(Array.isArray(arr)); // true — correct
console.log(Array.isArray(obj)); // false — correct
An element is added then removed — trace the array at each step before running.
let arr = [10, 20, 30];
arr.push(40);
arr.pop();
console.log(arr.length);
console.log(arr[2]);
// Start: arr = [10, 20, 30] length = 3
// idx: 0 1 2
// Step 1: arr.push(40)
// Adds 40 at the end
// arr = [10, 20, 30, 40] length = 4
// Step 2: arr.pop()
// Removes the last element (40)
// arr = [10, 20, 30] length = 3
// Step 3: console.log(arr.length)
// push added one, pop removed one — net change: zero
// Output → 3
// Step 4: console.log(arr[2])
// Index 2 holds the value 30
// Output → 30
// Final output:
// 3 ← length is back to its original value
// 30 ← arr[2] is the third element, unchanged
// Key takeaway:
// push() and pop() are mirror operations —
// performing both in sequence leaves the array unchanged.