JavaScript Arrays 101

Search for a command to run...

No comments yet. Be the first to comment.
These are the assignments or knowledge dumps I decide to write for the "Chai aur Code's web Dev Cohort 2026"
How does a computer remember things? Learn the basics of storing information in JavaScript, from let and const to strings and booleans.
RAG helps LLMs answer questions using your own data, but it doesn't guarantee correct answers. Here's how the pipeline works and where it commonly breaks down.

Imagine asking an AI, "Write a blog," versus "Act as an SEO expert and write a 1,000-word blog on AI safety formatted in markdown." The difference in the resulting output is night and day. A prompt is

Spotify and Apple Music make it incredibly easy to showcase your live listening habits on your website. YouTube Music? Not so much. After doing some setup you can do an API call and fetch your account

Arthur C. Clarke's famous third law: "Any sufficiently advanced technology is indistinguishable from magic" perfectly captures how the pace of human innovation works. From carrying supercomputers in o

Why HTTP is amnesiac, the difference between transport and state, and how to choose the right auth strategy for your Node.js app.
Imagine you are building a simple to-do list application. You need a way to store the user's tasks.
Using what you know about basic variables, you might try something like this:
let task1 = "Buy groceries";
let task2 = "Pay bills";
let task3 = "Walk the dog";
let task4 = "Read a book";
This works for four tasks. But what happens when the user has 100 tasks? What happens when they want to delete task number 2, or add a new task at the end? You cannot dynamically create new variables like task101 on the fly.
When you have a collection of related data, storing them in individual variables is a nightmare.
To solve this, we use an Array.
At its core, an array is just a list. It is a single variable that can hold multiple values at the same time, keeping them in a specific, ordered sequence.
In JavaScript, creating an array is as simple as wrapping your comma-separated values in square brackets [].
let tasks =["Buy groceries", "Pay bills", "Walk the dog", "Read a book"];
Now, instead of four scattered variables, you have one clean, organized box called tasks that holds everything. Arrays can hold anything: strings, numbers, booleans, and even other arrays.
If an array is a single box containing many items, how do you pull out just one specific item?
Every item inside an array is automatically assigned a numbered position, called an index. To get an item out, you write the name of the array, followed by square brackets containing that index number.
But here is the most important rule of arrays in almost every programming language: Counting starts at zero.
[ Visualizing the Array ]
Values: "Buy groceries" | "Pay bills" | "Walk the dog"
^ ^ ^
Index: 0 1 2
The first item is at index 0. The second item is at index 1.
let tasks =["Buy groceries", "Pay bills", "Walk the dog"];
console.log(tasks[0]); // Output: "Buy groceries"
console.log(tasks[2]); // Output: "Walk the dog"
Why zero? Historically, in older languages like C, an array's name pointed to a specific memory address on your computer. The index was the "offset" from that starting address. The first item is exactly at the starting address, so it is 0 steps away. The logic stuck, and now we all count from zero.
Arrays are mutable. This means you can change the data inside them after they are created.
To update an element, you access it by its index and assign it a new value using the equals sign, exactly like you would with a normal variable.
let tasks =["Buy groceries", "Pay bills", "Walk the dog"];
// We already paid the bills, let's update that task
tasks[1] = "Call the bank";
console.log(tasks);
// Output:["Buy groceries", "Call the bank", "Walk the dog"]
length PropertyOften, you won't know exactly how many items are in an array. Maybe you fetched a list of users from a database, and there could be 10, or there could be 10,000.
Every array in JavaScript comes with a built-in property called length that tells you exactly how many items it holds.
let fruits =["Apple", "Banana", "Mango", "Orange"];
console.log(fruits.length); // Output: 4
Notice a crucial detail here: The length is 4, but the highest index in the array is 3 (because we started counting at 0).
Because of this rule, you can always find the very last item in any array, no matter how big it is, by subtracting 1 from the length:
let lastFruit = fruits[fruits.length - 1];
console.log(lastFruit); // Output: "Orange"
The true power of an array is that it allows you to execute the same block of code for every item in the list. To do this, we use a for loop.
We can use our knowledge of indexes and the length property to tell the loop exactly when to start and when to stop.
let tasks = ["Buy groceries", "Pay bills", "Walk the dog"];
// Start 'i' at 0. Keep looping as long as 'i' is less than 3.
for (let i = 0; i < tasks.length; i++) {
console.log("Task " + (i + 1) + ": " + tasks[i]);
}
/*
Output:
Task 1: Buy groceries
Task 2: Pay bills
Task 3: Walk the dog
*/
In this loop, the variable i acts as our index. It starts at 0 (the first item), runs the code, and then increments by 1. It stops the moment it reaches tasks.length, ensuring we never try to access an index that doesn't exist.
Understanding how to access, update, and manually loop over arrays is a non-negotiable fundamental skill. You must know how the index works.
However, as you write more JavaScript, you will realize that writing for (let i = 0; i < array.length; i++) over and over gets tedious. Modern JavaScript provides powerful, built-in tools to manage arrays without ever writing a manual loop.
Once you are comfortable with everything in this post, you should step up to the next level. Check out my other blog on arrays: JavaScript Arrays: The 7 Methods You Actually Need to Know to see how professional developers map, filter, and mutate data.