TalentJR

🟨 JavaScript tutorial for beginners · Class 8 · Lesson 7 of 18 · Free

Arrays in JavaScript – Store and Use Lists of Data

⏱ About 30 minutes · ▶ run real JavaScript here — nothing to install · no sign-in

In this lesson you will

  • Create an array and read any item using its index
  • Change, add and remove items with push, pop, shift and unshift
  • Loop through an array to total marks and show a list on a web page

Your class teacher has the marks of 40 students. Making 40 separate variables (mark1, mark2, mark3 …) would be a nightmare. An array keeps the whole list in ONE variable, in order, ready to add, change or total.

What is an array in JavaScript?

An array is one variable that holds a list of values. You write the list inside square brackets [ ], with commas between the items.

The items stay in the same order you wrote them. An array can hold numbers, strings, or a mix.

Every array knows how many items it has. Ask it with .length.

🟨 JavaScript — you can edit this code
Console output
[78, 92, 65, 88]
["poha", "idli", "paratha"]
Number of marks: 4
Tiffin items: 3

💡 Try it: add a fifth mark like 99 inside the brackets and run again. Watch .length change.

How do I get one item from a JavaScript array? (Why does the index start at 0?)

Each item has a position number called its index. The first item is at index 0, not 1. Think of it as "how many steps from the start".

Write the array name, then the index in square brackets: players[0]. The last item is at index length − 1.

If you ask for an index that does not exist, JavaScript does not crash. It gives undefined, which means "nothing here".

🎬 How JavaScript thinks — step 1 of 9JavaScript is about to run line 1
▶const players = ["Rohit", "Virat", "Gill", "Pant"];
2console.log(players[0]);
3console.log(players[2]);
4const lastIndex = players.length - 1;
5console.log("Last index:", lastIndex);
6console.log(players[lastIndex]);
7console.log(players.at(-1));
8console.log(players[10]);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Rohit
Gill
Last index: 3
Pant
Pant
undefined

💡 .at(-1) is a newer, shorter way to get the last item. Negative numbers count from the end.

How do I change, add and remove items in an array?

To change an item, put a new value at its index: days[1] = "Wed".

push() adds an item at the end. pop() removes the last item and gives it back to you.

unshift() adds at the start. shift() removes the first item. Like a queue at the school canteen: the first person in line is served first.

Notice the array is const, but we can still change what is inside it. const only stops you from replacing the whole array with a new one.

🎬 How JavaScript thinks — step 1 of 13JavaScript is about to run line 1
▶const queue = ["Asha", "Ravi", "Meera"];
2queue[1] = "Kabir";
3console.log(queue);
4queue.push("Zoya");
5console.log("After push:", queue);
6const served = queue.shift();
7console.log("Served:", served);
8queue.unshift("Teacher");
9console.log("After unshift:", queue);
10const left = queue.pop();
11console.log("Left the line:", left);
12console.log(queue, queue.length);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
["Asha", "Kabir", "Meera"]
After push: ["Asha", "Kabir", "Meera", "Zoya"]
Served: Asha
After unshift: ["Teacher", "Kabir", "Meera", "Zoya"]
Left the line: Zoya
["Teacher", "Kabir", "Meera"] 3

How do I loop through an array in JavaScript?

A for…of loop visits every item, one by one, from first to last. Inside the loop, the variable holds the current item. (You will learn all loops properly in Lesson 9.)

This is how you total marks. Start a total at 0, then add each mark to it.

The average is the total divided by the number of items, which is .length.

🎬 How JavaScript thinks — step 1 of 13JavaScript is about to run line 1
▶const marks = [78, 92, 65, 88];
2let total = 0;
3for (const mark of marks) {
4 total = total + mark;
5}
6console.log("Total:", total);
7console.log("Average:", total / marks.length);
📦 Variables (memory boxes)

No variables yet.

🖥️ Output
▌
✏️ Change this code and run it yourself
🟨 JavaScript — you can edit this code
Console output
Total: 323
Average: 80.75

💡 Try it: push one more mark before the loop. The total and average update by themselves.

How do I check if an array has an item?

includes() answers yes or no: true if the item is in the array, false if not.

indexOf() tells you WHERE the item is. If it is not there, you get −1.

Both are case-sensitive: "Pune" and "pune" are different strings.

🟨 JavaScript — you can edit this code
Console output
true
false
2
-1
Mumbai → Lonavala → Pune → Satara

How do I show a JavaScript array on a web page?

Arrays and the DOM (Lesson 4) work well together. For each item, create an <li> and add it to a list.

Here the button pushes a new item into the array and draws the list again. The array is the "truth"; the page only shows it.

Click the button a few times and watch both the page and the console.

🟨 JavaScript — you can edit this code
📄 HTML of the page (tap to see / edit)
🖥️ Your web page — try clicking it
Console output
Menu now: ["Poha", "Idli", "Paratha"]

🤔 Predict the output

Think like JavaScript: read the code and choose what it prints. You get the reason straight away.

Question 1. What does this print?

const runs = [10, 20, 30];
console.log(runs[1]);

Question 2. What does this print?

const days = ["Mon", "Tue"];
days.push("Wed");
console.log(days.length);

Question 3. What does this print?

const nums = [5, 6, 7];
console.log(nums[3]);

🧩 Your challenge

Build a ₹ pocket-money tracker. Start with spends = [40, 25, 60]. Add a new spend of 30 with push. Then print how many times you spent, the total, and the first and last spend. Expected output: Spends: [40, 25, 60, 30] Times spent: 4 Total: ₹155 First: ₹40, Last: ₹30

Expected output in the console:
Spends: [40, 25, 60, 30]
Times spent: 4
Total: ₹155
First: ₹40, Last: ₹30
🟨 JavaScript — you can edit this code

⚠️ Common mistakes (and how to fix them)

❌ Wrong

const toppers = ["Asha", "Ravi", "Meera"];
console.log("First topper:", toppers[1]);

No error — it prints Ravi, the SECOND name, because index 1 is the second item.

✅ Right

const toppers = ["Asha", "Ravi", "Meera"];
console.log("First topper:", toppers[0]);

Array indexes start at 0, so the first item is [0].

❌ Wrong

const marks = [78, 92, 65];
console.log(marks.length());

TypeError: marks.length is not a function

✅ Right

const marks = [78, 92, 65];
console.log(marks.length);

length is a property (a stored value), not a function. Write it without brackets ().

❌ Wrong

const fruits = ["Mango", "Banana"];
fruits = ["Mango", "Banana", "Chikoo"];
console.log(fruits);

TypeError: Assignment to constant variable.

✅ Right

const fruits = ["Mango", "Banana"];
fruits.push("Chikoo");
console.log(fruits);

A const variable cannot point to a new array. But you can change the items inside it, so use push.

📌 Remember

  • An array stores a list of values in one variable: [1, 2, 3].
  • Indexes start at 0; the last item is at length − 1 (or .at(-1)).
  • A missing index gives undefined, not an error.
  • push/pop work at the end; unshift/shift work at the start.
  • for…of visits every item — perfect for totals and averages.
  • includes() says if an item is there; indexOf() says where (−1 if not).

Finished the quiz and the challenge?

Questions students ask

What is an array in JavaScript in simple words?

It is a list stored in one variable. You write the items inside square brackets, separated by commas. Each item has a position number called an index, starting from 0.

Why do JavaScript arrays start at index 0?

The index means "how many steps from the start". The first item is zero steps away, so it is at index 0. Most programming languages, including Python, work the same way.

Can a JavaScript array hold different types of values?

Yes. One array can hold numbers, strings, true/false, even other arrays. In real programs, it is usually clearer to keep one type of thing in each array.

Why can I push into a const array?

const means the variable always points to the same array. It does not freeze the items inside. So push and changing items work, but fruits = [...] with a new array gives an error.