Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
3 min read

When working with JavaScript, you often need to store multiple values together.

For example, imagine you want to store:

  • Your favorite movies

  • A list of fruits

  • Student marks

  • Daily tasks

Instead of creating many separate variables, JavaScript provides a simple structure called an array.

What Are Arrays ?

An array is a collection of values stored together in a single variable.

Example in real life:

Favorite Movies
1. Interstellar
2. Inception
3. Avengers
4. The Dark Knight
5. Spider-Man

Instead of writing 5 different variables like this:

let movie1 = "Interstellar";
let movie2 = "Inception";
let movie3 = "Avengers";

We can store them in one array.

let movies = ["Interstellar", "Inception", "Avengers"];

This makes code cleaner and easier to manage.

How to Create an Array

Arrays are created using square brackets [].

Example:

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

Here:

  • fruits is the array name

  • The values inside the brackets are array elements

You can also store numbers.

let marks = [85, 90, 78, 92];

Accessing Elements Using Index

Each value in an array has a position called an index.

Important rule:

Array indexing starts from 0, not 1.

Example array:

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

Access elements like this:

console.log(fruits[0]);
console.log(fruits[1]);
console.log(fruits[2]);

Output:

Apple
Banana
Mango

So:

Index Value
0 Apple
1 Banana
2 Mango

Updating Array Elements

You can change any element using its index.

Example:

let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Grapes";
console.log(fruits);

Output:

["Apple", "Grapes", "Mango"]

Here we changed Banana → Grapes.

Array Length Property

JavaScript arrays have a built-in property called length.

It tells us how many elements are in the array.

Example:

let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits.length);

Output:

4

This is very useful when working with loops.

Looping Over Arrays

If an array has many values, printing them one by one would be tedious.

Instead, we can use a loop.

Example using a for loop:

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

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

Output:

Apple
Banana
Mango
Orange

The loop runs through every element of the array.

Simple one Example

Let’s create a list of tasks.

let tasks = ["Study JavaScript", "Go to gym", "Complete assignment"];

console.log(tasks[0]);
console.log(tasks[1]);
console.log(tasks[2]);

We can also loop through them.

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

Arrays are one of the most useful data structures in JavaScript because they allow us to store and manage multiple values efficiently.

Once you understand arrays, you can start working with more advanced operations like filtering, mapping, and transforming data.

But mastering the basics of arrays is the first step toward writing better JavaScript programs.