Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays Explained

Updated
3 min read

When writing programs, we often need to store multiple values together.

For example, imagine you want to store:

  • A list of fruits

  • A student's marks

  • A list of tasks

One way is to create separate variables:

let fruit1 = "apple";
let fruit2 = "banana";
let fruit3 = "mango";

But this quickly becomes messy and hard to manage.

A better solution is to store all values inside a single structure called an array.

In this article, we will learn:

  • What arrays are and why we need them

  • How to create an array

  • How to access elements using index

  • How to update elements

  • The array length property

  • Basic looping over arrays

All examples are simple so you can try them in your browser console or Node.js.


What Are Arrays?

An array is a collection of values stored in order.

Instead of creating multiple variables, we store the values inside one array.

Example:

let fruits = ["apple", "banana", "mango"];

Here the array contains three values.

Output:

["apple", "banana", "mango"]

So instead of writing many variables, we use one array to store multiple items.


Creating an Array

Arrays are created using square brackets [].

Example:

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

Example with strings:

let fruits = ["apple", "banana", "mango"];

Arrays can also contain different types of values:

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

Accessing Array Elements

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

Important rule:

Array indexing starts from 0

Example:

let fruits = ["apple", "banana", "mango"];

Index positions:

0 → apple
1 → banana
2 → mango

Accessing elements:

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

Output:

apple
banana

Updating Array Elements

We can change an element in an array by using its index.

Example:

let fruits = ["apple", "banana", "mango"];

fruits[1] = "orange";

console.log(fruits);

Output:

["apple", "orange", "mango"]

Here the value banana was replaced with orange.


The Array length Property

The length property tells us how many elements are in the array.

Example:

let fruits = ["apple", "banana", "mango"];

console.log(fruits.length);

Output:

3

This is very useful when working with loops.


Looping Over Arrays

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

We can do this using a for loop.

Example:

let fruits = ["apple", "banana", "mango"];

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

Output:

apple
banana
mango

How it works:

  1. i starts from 0

  2. The loop runs while i < fruits.length

  3. Each iteration prints fruits[i]

So the loop accesses every element in the array.


Practical Example

Example with student marks:

let marks = [75, 82, 90, 68];

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

Output:

75
82
90
68

Arrays are one of the most important data structures in JavaScript.

They allow us to store multiple values in a single variable and work with them easily.

Key things to remember:

  • Arrays store multiple values in order

  • They are created using square brackets []

  • Elements are accessed using indexes starting from 0

  • The length property tells the number of elements

  • Loops help us process every item in an array