Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
4 min readView as Markdown
JavaScript Arrays 101

A simple guide to understanding JavaScript arrays. In this article we will learn how arrays help us store multiple values, access them, update them, and loop through them using easy real-life examples.


Why Do We Need Arrays?

Suppose you want to store a list of your favorite movies:

  • Inception

  • Interstellar

  • Avatar

  • Titanic

If you try to store them without arrays, your code might look like this:

let movie1 = "Inception";
let movie2 = "Interstellar";
let movie3 = "Avatar";
let movie4 = "Titanic";

This works for a small list. But if the list becomes longer, managing many variables becomes confusing and messy.

This is exactly why arrays are useful.

An array lets us store multiple values inside one single variable, making the code more organized.


What Are Arrays in JavaScript?

An array is simply a group of values stored together in a specific order.

Example:

let cities = ["Delhi", "Mumbai", "Jaipur", "Chennai"];

Here:

  • cities is the array

  • "Delhi", "Mumbai", "Jaipur", "Chennai" are the elements inside the array

Arrays are commonly used for storing lists like:

  • City names

  • Shopping items

  • Student names

  • Favorite songs

Real-life analogy

Think of an array like a train with multiple compartments.

Each compartment stores passengers, and each one has its own number.


How to Create an Array

In JavaScript, arrays are created using square brackets [ ].

Example:

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

Another example:

let animals = ["Dog", "Cat", "Elephant"];

Each item inside the brackets is separated by a comma.


Understanding Array Positions (Index)

Every element inside an array has a position number called an index.

Important rule:

Arrays always start indexing from 0, not from 1.


Accessing Elements Using Index

We can access any element by using its index number.

Example:

let animals = ["Dog", "Cat", "Elephant", "Lion"];

console.log(animals[0]);

Output:

Dog

Accessing another element:

console.log(animals[2]);

Output:

Elephant

Real-life example

Imagine a parking lot where spots are numbered starting from 0 instead of 1.

Each vehicle is parked in a specific numbered spot.


Updating Array Elements

We can also change any element in the array using its index.

Example:

let animals = ["Dog", "Cat", "Elephant"];

animals[1] = "Rabbit";

console.log(animals);

Output:

["Dog", "Rabbit", "Elephant"]

Here we replaced Cat with Rabbit.

Real-life example

Think about editing an item in your grocery list when you decide to buy something else.


The Array Length Property

The length property tells us how many elements exist in an array.

Example:

let animals = ["Dog", "Cat", "Elephant", "Lion"];

console.log(animals.length);

Output:

4

The length property becomes very helpful when we use loops.


How Arrays Are Stored

Internally, each element of an array is stored in separate memory locations, and each location is identified by its index number.


Looping Through Arrays

Often we need to go through every element in an array.

The for loop is commonly used for this purpose.

Example:

let animals = ["Dog", "Cat", "Elephant"];

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

Output:

Dog
Cat
Elephant

Explanation

  • The loop starts at index 0

  • It continues until it reaches the array length

  • Each element is printed one by one

Real-life example

Think about checking every item in your backpack before leaving for school.


Why Arrays Are Useful

Arrays help programmers:

  • Store many values in a single variable

  • Keep data organized

  • Easily loop through items

  • Manage large lists efficiently

Without arrays, handling large amounts of data would be much harder.


Practice Assignment

Step 1: Create an Array of 5 Favorite Foods

let foods = ["Pizza", "Burger", "Pasta", "Biryani", "Noodles"];

Step 2: Print the First and Last Item

console.log(foods[0]);
console.log(foods[foods.length - 1]);

Step 3: Change One Value

foods[2] = "Sandwich";

console.log(foods);

Step 4: Loop Through the Array

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