JavaScript Arrays for Beginners
Learn to store, access, update, and loop through data in JavaScript — no experience needed.

👋 Hey, I'm Mohd Kaif – a student documenting my journey through code. I write about what I'm learning in real-time – the wins, the struggles, and the "aha!" moments. From JavaScript and React to backend systems with Node.js, databases, DevOps, TypeScript, and AI integrations. This blog is my public learning journal: honest, evolving, and always exploring. If you're curious about any of these topics, let's learn and build together!
You know that moment when your code starts looking like a grocery receipt that never ends?
Are you creating a new variable every time you need to store one more value?
Have you ever scrolled through your own code and thought, "There has to be a better way"?
Do you find yourself copy-pasting the same logic over and over — just with a different variable name?
Are you learning JavaScript but feel like something fundamental hasn't clicked yet?
If any of that sounds familiar — you're not behind, and you're definitely not alone.
The problem isn't that you're a bad programmer. The problem is that nobody showed you arrays early enough.
If you've ever wrestled with managing multiple values in JavaScript, this article is written specifically for you.
What You'll Learn ✅
✅ What arrays are and why they exist in the first place
✅ How to create an array from scratch
✅ How to access and update values using indexes
✅ Why indexing starts at 0 — and how to stop getting tripped up by it
✅ How to use the length property to write smarter, safer code
✅ How to loop through arrays to work with every element automatically
No prerequisites required. If you know what a variable is, you're ready for this.
The Problem Arrays Are Solving
Imagine you're building a simple app that tracks a student's scores across five subjects.
Without arrays, you'd probably write something like this:
let score1 = 85;
let score2 = 90;
let score3 = 78;
let score4 = 92;
let score5 = 88;
Now ask yourself — what happens when you need to:
Add a sixth subject?
Find the average of all scores?
Print every score with a single block of code?
You're stuck. You'd have to manually touch every variable, every time. This approach doesn't scale — and real software always scales.
Arrays fix this entirely.
What Exactly Is an Array?
An array is a single variable that holds an ordered collection of values.
Think of it like a row of labeled storage boxes:
| Index | [0] |
[1] |
[2] |
[3] |
[4] |
|---|---|---|---|---|---|
| Value | 85 | 90 | 78 | 92 | 88 |
Each box holds a value. Each box has a number label called an index. You use that index to reach in and grab exactly what you need.
With arrays, those five messy score variables become one clean line:
let scores = [85, 90, 78, 92, 88];
Same data. One variable. Infinitely more manageable.
How to Create an Array
Creating an array in JavaScript is straightforward — use square brackets [] and separate values with commas.
// An array of strings
let fruits = ["Apple", "Banana", "Mango"];
// An array of numbers
let scores = [85, 90, 78];
// An array of mixed types (valid, though uncommon)
let mixed = ["Alice", 25, true];
💡 Quick tip: Arrays can store strings, numbers, booleans — and even other arrays. For now, stick to one type per array to keep things readable.
🏋️ Quick Exercise #1
Open your browser console (press F12 → click "Console") and type:
let cities = ["New York", "Tokyo", "London", "Sydney"];
console.log(cities);
You should see the array printed out. Notice the indexes listed next to each item? That's JavaScript showing you the position of every element.
Accessing Elements Using Index
Here's the single most important thing to burn into memory:
Array indexes start at 0, not 1.
This trips up nearly every beginner at least once. Let's make sure it doesn't trip you up twice.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]); // "Apple" ← first element
console.log(fruits[1]); // "Banana" ← second element
console.log(fruits[2]); // "Mango" ← third element
Visualizing it helps:
| Index | [0] |
[1] |
[2] |
|---|---|---|---|
| Value | "Apple" |
"Banana" |
"Mango" |
The index is just the distance from the start. The first item is 0 steps away from the beginning — hence index 0. It's an offset, not a count.
Once that mental model clicks, you'll never mix it up again.
🏋️ Quick Exercise #2
Given this array:
let colors = ["Red", "Green", "Blue", "Yellow", "Purple"];
Without running the code first, predict:
What does
colors[0]return?What does
colors[4]return?What does
colors[5]return?
Then run it and check your answers. (Hint: one of them might surprise you.)
Updating Elements in an Array
Arrays are mutable — which means you can change their contents after creating them.
To update a value, just target it by index and assign a new one:
let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Orange"; // Replace "Banana" with "Orange"
console.log(fruits); // ["Apple", "Orange", "Mango"]
You're not replacing the whole array — just swapping out the value at that specific position. Everything else stays exactly where it was.
The length Property — More Useful Than It Looks
Every array comes with a built-in .length property that tells you how many elements it contains.
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.length); // 3
Simple enough. But here's where it gets genuinely useful.
Getting the last element — without hardcoding the index:
let lastFruit = fruits[fruits.length - 1];
console.log(lastFruit); // "Mango"
Why length - 1? Because if there are 3 items, their indexes are 0, 1, and 2. The last index is always one less than the total count.
length = 3
last index = 3 - 1 = 2
fruits[2] = "Mango" ✓
This pattern — array[array.length - 1] — is one of the most commonly used in real JavaScript code. Get comfortable with it now.
⚠️ Common trap:
fruits[fruits.length]returnsundefined. The length is a count, not a valid last index.
Looping Over Arrays
This is where arrays go from useful to powerful.
Instead of manually accessing each element one by one, you can use a loop to run the same code on every element automatically.
The for Loop
let fruits = ["Apple", "Banana", "Mango"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// Output:
// Apple
// Banana
// Mango
Here's what's happening on each step:
| Loop Step | i value |
fruits[i] |
|---|---|---|
| 1st pass | 0 | "Apple" |
| 2nd pass | 1 | "Banana" |
| 3rd pass | 2 | "Mango" |
| Loop ends | 3 | (3 is not < 3, so we stop) |
The condition i < fruits.length is the key — it makes sure you never accidentally go past the end of the array.
🏋️ Quick Exercise #3
Take this array of temperatures:
let temps = [22, 31, 19, 27, 35, 28];
Write a for loop that prints every temperature. Then, as a bonus challenge, modify the loop to only print temperatures above 25.
Individual Variables vs. Arrays: Side by Side
Let's bring it all together with a real comparison.
❌ The old way — five separate variables
let score1 = 85;
let score2 = 90;
let score3 = 78;
let score4 = 92;
let score5 = 88;
// To print all of them? Write it five times. Manually.
console.log(score1);
console.log(score2);
// ...and so on
✅ The array way — one variable, full control
let scores = [85, 90, 78, 92, 88];
// Print all scores with a loop
for (let i = 0; i < scores.length; i++) {
console.log(scores[i]);
}
// Add a new score? One line.
scores[5] = 95;
// Find the total? Entirely possible now.
The array version isn't just shorter — it's structured in a way that allows your code to grow without falling apart.
Common Mistakes (and How to Avoid Them)
1. Off-by-one: Forgetting indexes start at 0
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[1]); // "Banana" — NOT the first item
Fix: Always remind yourself — index 0 is first, index 1 is second.
2. Accessing an index that doesn't exist
let fruits = ["Apple", "Banana"];
console.log(fruits[5]); // undefined — no error, just nothing
JavaScript won't throw an error here — it silently returns undefined. This can cause bugs that are hard to track down. Fix: Check array.length before accessing an index if you're unsure.
3. Confusing length with the last index
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.length); // 3 ← count of items
console.log(fruits[fruits.length]); // undefined ← no such index!
console.log(fruits[fruits.length - 1]); // "Mango" ← correct ✓
Fix: The last valid index is always length - 1. Always.
Practice Assignment
Put everything together with this mini project.
Step 1 — Create your array
let movies = ["Inception", "Interstellar", "Avatar", "Titanic", "Joker"];
Step 2 — Print the first and last element
console.log(movies[0]); // First
console.log(movies[movies.length - 1]); // Last
Step 3 — Update one value
movies[2] = "The Dark Knight";
console.log(movies);
Step 4 — Loop through all movies
for (let i = 0; i < movies.length; i++) {
console.log(movies[i]);
}
Bonus challenge: Modify the loop to also print the index next to each movie title, like:
0: Inception
1: Interstellar
...
What to Learn Next
You've got the foundation — here's the natural path forward:
Array Methods (
push,pop,shift,unshift) — Add and remove elements dynamically instead of only updating them in place.forEachandmap— Cleaner, more modern ways to loop over arrays that you'll see constantly in real JavaScript projects.Filtering and Finding (
filter,find) — Search through arrays and extract only the values that match a condition.Multi-dimensional Arrays — Arrays that contain other arrays, used for grids, tables, and matrix data.
Each of these builds directly on what you learned here. Master the basics in this article first, and the next steps will feel obvious rather than overwhelming.
💬 Got Questions?
Drop a comment below — I'd love to help you work through any part of this that didn't click, or hear about something you built using arrays!
Here are the topics coming up in this series:
Array Methods Deep Dive: A hands-on look at
push,pop,splice, and friends — the tools you'll use in almost every real project.Loops Compared (
forvsforEachvsfor...of): Which loop should you actually use, and when? Explained with clear examples.Functions in JavaScript: How to stop repeating yourself by packaging logic into reusable, callable blocks.
Objects vs Arrays: When to use which, and how they work together in real JavaScript applications.
Found this helpful? Share it with someone who's just starting their JavaScript journey — it might be exactly what they needed to read today.




