Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
β€’6 min readβ€’View as Markdown
JavaScript Arrays 101

What is an Array?

An array is a container that stores multiple values in one variable.

Think of it like a basket 🧺 where you keep many fruits together instead of holding them separately.

Example without array:

let fruit1 = "Apple";
let fruit2 = "Banana";
let fruit3 = "Mango";

This becomes messy when there are many values.

Example with array:

let fruits = ["Apple", "Banana", "Mango"];

Now everything is inside one variable.

2️⃣ Slightly Practical Example (Student Marks)

Imagine a teacher storing marks of students.

Without array:

let mark1 = 78;
let mark2 = 82;
let mark3 = 90;
let mark4 = 65;

With array:

let marks = [78, 82, 90, 65];

Now we can easily calculate average marks.

let total = 0;

for(let i = 0; i < marks.length; i++){
    total += marks[i];
}

let average = total / marks.length;

console.log(average);

So arrays help us loop through data easily.


3️⃣ Real World Example (Shopping Cart)

Think of an E-commerce cart.

A user adds many products.

let cart = ["Shoes", "Tshirt", "Watch", "Cap"];

Add item:

cart.push("Sunglasses");

Remove item:

cart.pop();

Check total items:

console.log(cart.length);

This is how shopping cart systems work internally.


4️⃣ Real World Example (Instagram Likes)

Imagine storing people who liked a post.

let likes = ["Rahul", "Priya", "Aman", "Sneha"];

Display likes:

likes.forEach(function(user){
   console.log(user + " liked your post");
});

Output:

Rahul liked your post
Priya liked your post
Aman liked your post
Sneha liked your post

5️⃣ Advanced Real World Example (Products in Website)

Example similar to what you may use in your UI work (like product cards).

let products = [
   "Nike Shoes",
   "Adidas T-shirt",
   "Puma Jacket",
   "Woodland Boots"
];

products.forEach(function(product){
   console.log(product);
});

This is how product lists are rendered on websites.


πŸ“Œ Why We Need Arrays

Arrays help us:

1️⃣ Store multiple values in one variable

2️⃣ Loop through data easily

3️⃣ Add / remove items dynamically

4️⃣ Manage lists (products, users, comments, likes, cart items)

5️⃣ Build dynamic UI


Memory-Style Block Diagram of Array Storage

When an array is stored in memory, the values are placed next to each other in continuous memory locations.

Example Array:

let numbers = [10, 20, 30, 40];

Memory Representation

Index:      0        1        2        3
          -----    -----    -----    -----
Memory β†’ | 10 | -> | 20 | -> | 30 | -> | 40 |
          -----    -----    -----    -----

Or a simpler block diagram:

        Array: numbers

        β”Œβ”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”
Index β†’ β”‚  0  β”‚  1  β”‚  2  β”‚  3  β”‚
        β”œβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€
Value β†’ β”‚ 10  β”‚ 20  β”‚ 30  β”‚ 40  β”‚
        β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”˜

Each block represents a memory slot storing one value.


3️⃣ Real World Example

Imagine a train with numbered seats.

Seat No:   0     1     2     3
          β”Œβ”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”¬β”€β”€β”€β”
People β†’  β”‚ A β”‚ B β”‚ C β”‚ D β”‚
          β””β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”΄β”€β”€β”€β”˜
  • Seat number = index

  • Person = value

To find person C:

train[2]

4️⃣ Key Points

βœ” Array stores multiple values in one variable

βœ” Values are stored in order

βœ” Each value has an index starting from 0

βœ” Stored in continuous memory locations

Why Do We Need Arrays?

Imagine you are building a music player website where users save their favorite songs.

If you store songs individually, it becomes messy.

Without Array (Storing Values Individually)

let song1 = "Shape of You";
let song2 = "Blinding Lights";
let song3 = "Perfect";
let song4 = "Believer";

Problems:

  • Hard to manage when songs increase

  • Difficult to loop through songs

  • Code becomes repetitive

Using an Array

An array stores multiple values in one variable.

let playlist = ["Shape of You", "Blinding Lights", "Perfect", "Believer"];

Now all songs are stored in one place.

How Indexing Works (Starts from 0)

In JavaScript, array positions start from 0, not 1.

Index Value
0 Shape of You
1 Blinding Lights
2 Perfect
3 Believer

So:

playlist[0] // Shape of You
playlist[1] // Blinding Lights
let playlist = ["Shape of You", "Blinding Lights", "Perfect", "Believer"];

console.log(playlist[0]);

Output

Shape of You
let playlist = ["Shape of You", "Blinding Lights", "Perfect", "Believer"];

console.log(playlist[3]);

Output

Believer

Change a Value in the Array

Arrays allow updating values easily.

let playlist = ["Shape of You", "Blinding Lights", "Perfect", "Believer"];

playlist[1] = "Levitating";

console.log(playlist);

Output

["Shape of You", "Levitating", "Perfect", "Believer"]

Loop Through the Array

You can print all elements automatically using a loop.

let playlist = ["Shape of You", "Blinding Lights", "Perfect", "Believer"];

for (let i = 0; i < playlist.length; i++) {
    console.log(playlist[i]);
}

Output

Shape of You
Blinding Lights
Perfect
Believer

Individual Variables vs Array

Feature Individual Variables Array
Storage Each value stored separately All values stored in one variable
Code Length Longer and repetitive Short and clean
Looping Difficult Very easy
Scalability Hard to manage large data Easily handles many values
Maintenance Hard to update Easy to update

Pros Cons
Stores multiple values in one variable Index starts from 0 which may confuse beginners
Easy to loop through data Wrong index can cause errors
Easy to update elements Large arrays may need careful management
Cleaner and organized code Not suitable for complex structured data alone

Simple Real-World Use Cases of Arrays

Arrays are commonly used in:

Example Explanation
Music Playlist Store list of songs
Shopping Cart Store items added by user
Blog Website Store blog titles
Online Course Store lessons
Photo Gallery Store image URLs

Quick Summary

  • Arrays store multiple values in one variable

  • Indexing starts from 0

  • You can access, update, and loop through elements easily

  • Arrays make code cleaner and easier to manage

Example:

let playlist = ["Shape of You", "Blinding Lights", "Perfect", "Believer"];
1 views