JavaScript Arrays 101

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
Search for a command to run...

Learning web development in public. Writing simple, real-world explanations about web development concepts. Helping beginners understand why things work, not just how.
No comments yet. Be the first to comment.
Every day, millions of users upload photos, videos, stories, and reels to Instagram. From a user's perspective, the process appears simple: select media, apply filters, add a caption, and tap "Post."
Building Offline-First Messaging Apps: How Messages Work Without Internet Modern messaging applications have transformed the way people communicate. Whether it's chatting with friends, collaborating w
In this article we'll explore about the Expo Router and React Navigation and answer which one to use in 2026. If you build mobile apps using React Native, one thing becomes obvious very quickly: Navig

Modern mobile apps are no longer just a collection of screens connected together. Apps like Instagram, WhatsApp, Uber, and Netflix operate at massive scale with millions of users, real time systems, o
In this article we'll be exploring react.js and the things of react.js that makes it popular and stand out among other libraries ( no fight over library vs framework ). We'll go through: What problem

Shkaai
68 posts
In JavaScript, an array is simply a collection of data stored in a single variable. It is hard to impossible to store large amount of data in different variables. And this where array comes in picture. Arrays help us store and manage multiple pieces of data in an organized way.
It is hard and inefficient to store large amounts of related data in different variables.
For example:
let fruit1 = "Apple";
let fruit2 = "Banana";
let fruit3 = "Mango";
This approach becomes difficult to manage when the number of items increases.
Instead, we can use an array:
let fruits = ["Apple", "Banana", "Mango"];
Now all related data is stored inside one variable, which makes the code cleaner and easier to work with.
Arrays are created using square brackets [].
Example:
let fruits = ["Apple", "Banana", "Mango"];
In this array:
"Apple" is the first element
"Banana" is the second element
"Mango" is the third element
Arrays can store different types of values as well.
Example:
let data = ["Sam", 25, true];
Each value inside an array has a position called an index.
Important thing to remember:
Array indexing starts from 0 in JavaScript.
Example:
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);
Output:
Apple
Banana
Mango
We can update an element by assigning a new value to its index.
Example:
let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Orange";
console.log(fruits);
Output:
["Apple", "Orange", "Mango"]
Here we changed "Banana" to "Orange".
JavaScript arrays have a built-in property called length. It tells us how many elements are inside the array.
Example:
let fruits = ["Apple", "Banana", "Mango"];
console.log(fruits.length);
Output:
3
This means the array contains 3 elements.
Very often we need to access all elements of an array. Instead of writing multiple console statements, we can use a loop.
One common way is using a for loop.
Example:
let fruits = ["Apple", "Banana", "Mango"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Output:
Apple
Banana
Mango
The loop starts from index 0 and continues until it reaches the end of the array.
Imagine storing marks of a student.
let marks = [75, 80, 92, 68];
console.log(marks[0]);
console.log(marks[2]);
Output:
75
92
Arrays are one of the most commonly used data structures in JavaScript. They allow us to store multiple values in a single variable and access them using indexes. By understanding how to create arrays, access elements, update values, check their length, and loop through them, we can handle collections of data much more efficiently in our programs.