Arrays & Objects Lesson 18 of 30
JavaScript Arrays
Store lists of values in arrays — create them, read and change items by index, get the length, add and remove items with push, pop, shift, unshift and splice.
A class has 40 students. Making 40 separate variables (student1, student2…) would be a nightmare. An array stores many values in a single variable, in order, like a numbered list.
Creating an array
const subjects = ["Math", "Science", "English", "Filipino"];
const grades = [88, 91, 85, 93];
const empty = [];
Items go inside square brackets [ ], separated by commas. An array can hold any type, even mixed types, but usually all items are the same kind.
Reading items by index
Each item has an index (position), starting at 0:
<pre id="out"></pre>
<script>
const subjects = ["Math", "Science", "English", "Filipino"];
document.getElementById("out").textContent =
"First: " + subjects[0] + "\n" +
"Second: " + subjects[1] + "\n" +
"Last: " + subjects[subjects.length - 1] + "\n" +
"Number of subjects: " + subjects.length + "\n" +
"Index 10: " + subjects[10];
</script>
Reading an index that doesn't exist gives undefined, not an error. You can also use subjects.at(-1) to get the last item.
Changing items
const subjects = ["Math", "Science", "English"];
subjects[1] = "Biology"; // ["Math", "Biology", "English"]
Adding and removing items
| Method | What it does |
|---|---|
push(x) | Add to the end |
pop() | Remove from the end (and return it) |
unshift(x) | Add to the start |
shift() | Remove from the start (and return it) |
splice(i, n) | Remove n items starting at index i |
<pre id="out"></pre>
<script>
const queue = ["Ana", "Ben"];
let log = "Start: " + queue.join(", ") + "\n";
queue.push("Carla");
log += "Carla joins: " + queue.join(", ") + "\n";
const served = queue.shift();
log += served + " is served: " + queue.join(", ") + "\n";
queue.unshift("Teacher");
log += "Teacher goes first: " + queue.join(", ") + "\n";
queue.splice(1, 1);
log += "Ben leaves: " + queue.join(", ");
document.getElementById("out").textContent = log;
</script>
Building a list on the page
<input id="item" placeholder="Add an item, e.g. Bigas">
<button onclick="add()">Add</button>
<ul id="list"></ul>
<p id="count"></p>
<script>
const items = [];
function add() {
const box = document.getElementById("item");
const text = box.value.trim();
if (text === "") return;
items.push(text);
box.value = "";
render();
}
function render() {
const list = document.getElementById("list");
list.innerHTML = "";
for (const it of items) {
const li = document.createElement("li");
li.textContent = it;
list.appendChild(li);
}
document.getElementById("count").textContent = items.length + " item(s) on the list";
}
</script>
This pattern is the basis of almost every web app: keep your data in an array, and write a render function that draws the array on the page whenever it changes.
Useful array basics
const fruits = ["mangga", "saging", "pinya"];
fruits.join(", ") // "mangga, saging, pinya"
fruits.includes("saging") // true
fruits.indexOf("pinya") // 2
fruits.reverse() // ["pinya", "saging", "mangga"] (changes the array)
Array.isArray(fruits) // true
Arrays of arrays
An array can contain other arrays, which is useful for tables and grids, like a seating chart:
const seats = [
["Ana", "Ben", "Carla"],
["Dan", "Ela", "Fe"],
];
seats[1][2] // "Fe" (row 1, seat 2)
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04