# JavaScript Arrays 101

Web Dev Cohort 2026 — JavaScript

When you first learn JavaScript, you usually store values like this:

*   one variable for name
    
*   one variable for age
    
*   one variable for city
    

That works… until you need to store **a list of things**.

What if you want to store:

*   5 favorite movies
    
*   10 student marks
    
*   20 tasks
    

Creating 20 separate variables would be painful.

This is where **arrays** come in.

* * *

## So, What Is an Array?

An array is simply a **collection of values stored in order**.

Think about a list of fruits:

Apple  
Banana  
Mango  
Orange

Instead of storing each fruit in a separate variable, we put them into one structure — an array.

You can imagine an array like a row of boxes:

\[ Apple | Banana | Mango | Orange \]

Each box holds one value, and each box has a position number.

* * *

## Why We Need Arrays

Without arrays:

let movie1 = "Inception"  
let movie2 = "Interstellar"  
let movie3 = "Tenet"

This quickly becomes messy.

With an array:

You keep everything organized in one place.

Arrays help when:

*   you have multiple related values
    
*   order matters
    
*   you want to loop through data
    

Almost every real application uses arrays somewhere.

* * *

## How to Create an Array

Creating an array is simple.

You use square brackets and separate values with commas.

Example idea:

An array of numbers  
An array of names  
An array of mixed values

Everything goes inside square brackets.

That’s it.

* * *

## Understanding Index (Important)

Arrays use something called **index**.

Index means position number.

But here’s the part beginners usually forget:

Array indexing starts from **0**, not 1.

So if you have:

\[ Apple, Banana, Mango \]

The positions are:

Apple → index 0  
Banana → index 1  
Mango → index 2

It may feel strange at first, but you’ll get used to it quickly.

* * *

## Accessing Elements

If you want to get a value from an array, you use its index.

For example:  
To get the first element → use index 0  
To get the second element → use index 1

So index acts like the address of that value.

* * *

## Updating Elements

Arrays are not fixed.

You can change values by using their index.

If you want to replace "Mango" with "Grapes", you just update that index position.

The array stays the same structure, only the value changes.

* * *

## The Length Property

Arrays have something very useful called **length**.

Length tells you how many items are inside the array.

If an array has 5 values, length will return 5.

This becomes very helpful when looping.

* * *

## Looping Over Arrays

Very common situation:  
You want to print all values in an array.

Instead of accessing each index manually, you use a loop.

The loop starts from index 0  
Runs until index is less than array length  
And prints each value one by one

This is how most real programs handle arrays.
