Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101: A Beginner’s Guide

Updated
6 min read

When learning JavaScript, one of the first challenges beginners face is managing multiple pieces of data. Imagine you want to store a list of your favorite movies or a set of student marks. Creating separate variables for each value quickly becomes messy and hard to manage.

That’s where arrays come in.

Arrays allow us to store multiple values inside a single variable, making our code cleaner and easier to work with.

In this article, we’ll learn the basics of arrays in JavaScript, including how to create them, access elements, update values, and loop through them.

The Problem Without Arrays

Let’s say you want to store five of your favorite movies.

Without arrays, you might write code like this:

let movie1 = "Inception";
let movie2 = "Interstellar";
let movie3 = "The Dark Knight";
let movie4 = "Avatar";
let movie5 = "Titanic";

This works, but it quickly becomes inconvenient.

If you wanted to print all the movies, you would need to write multiple console.log() statements. Managing many variables also makes your code harder to read and maintain.

A better solution is to store all these values together in one place.

That is exactly what arrays help us do.

What is an Array?

An array is a data structure that allows you to store multiple values in a single variable.

The values inside an array are stored in order, and each value has a position called an index.

Here is an example of an array:

let movies = ["Inception", "Interstellar", "The Dark Knight", "Avatar", "Titanic"];

In this example:

  • movies is the array name

  • The values inside the square brackets [ ] are the elements of the array

Arrays make it easier to manage lists of related data.

You can think of an array like a playlist of songs, where each song has a specific position in the list.

Why Do We Need Arrays?

Arrays are useful when working with collections of data.

Instead of creating many separate variables, we can store everything in one structured list.

For example, instead of this:

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

We can write:

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

This makes the code:

  • Easier to read

  • Easier to manage

  • Easier to process using loops

Arrays become extremely useful when handling lists such as:

  • Product lists

  • Student names

  • Tasks in a to-do app

  • Scores in a game

How to Create an Array in JavaScript

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

Example:

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

You can store different types of values in an array.

Example:

let data = ["John", 25, true];

However, beginners usually store similar types of data in one array for better organization.

For example:

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

Understanding Array Index (Starts from 0)

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

Important thing to remember:

Array indexing in JavaScript starts from 0, not 1.

Example array:

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

Index positions:

Index:   0       1        2
Value: Apple  Banana   Orange

So:

  • Apple is at index 0

  • Banana is at index 1

  • Orange is at index 2

This is a very common concept in programming languages.

Accessing Array Elements

To access a value from an array, we use the index number inside square brackets.

Example:

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

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

Output:

Apple
Banana

We can also access the last element if we know its index.

console.log(fruits[2]);

Output:

Orange

Updating Array Elements

We can also change or update values inside an array.

To update a value, we assign a new value to a specific index.

Example:

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

fruits[1] = "Mango";

console.log(fruits);

Output:

["Apple", "Mango", "Orange"]

Here we replaced Banana with Mango.

The Array Length Property

JavaScript arrays have a built-in property called length.

It tells us how many elements are inside the array.

Example:

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

console.log(fruits.length);

Output:

3

This property is useful when we want to loop through all elements in the array.

Looping Through Arrays

Instead of printing elements one by one, we can use a loop to go through all items in the array.

Example using a for loop:

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

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

Explanation:

  • i starts from 0

  • The loop runs until i is less than the array length

  • Each iteration prints the element at that index

Output:

Apple
Banana
Orange

This makes it easy to work with arrays that contain many values.

Practice Example

Let’s create an array of five favorite movies.

let movies = ["Inception", "Interstellar", "The Dark Knight", "Avatar", "Titanic"];
console.log(movies[0]);
console.log(movies[4]);

Change one movie

movies[2] = "Spider-Man";
console.log(movies);

Loop through the array

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

This simple exercise helps beginners practice the core concepts of arrays.

Visualizing an Array

Here is a simple way to visualize how arrays store values.

Index:   0        1        2        3        4
Value: Inception Interstellar DarkKnight Avatar Titanic

You can imagine an array as a row of boxes in memory, where each box stores a value and has a number called an index.

Conclusion

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

In this article, we learned:

  • What arrays are

  • Why they are useful

  • How to create arrays

  • How indexing works

  • How to access and update elements

  • How to use the length property

  • How to loop through arrays

Understanding arrays is a key step toward becoming comfortable with JavaScript. Once you are familiar with these basics, you will be able to work with more advanced array features and build more dynamic applications.