Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
3 min read
JavaScript Arrays 101

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

For example, imagine you want to store a list of fruits:

  • Apple

  • Mango

  • Banana

  • Orange

You could store them like this:

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

This works, but as the list grows, managing many variables becomes difficult.

This is where arrays become useful.

An array allows you to store multiple values in a single structure.

1. What Arrays Are and Why We Need Them

An array is a collection of values stored in a specific order.

Instead of creating many variables, you can store everything inside one array.

Example:

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

Now all the fruit names are stored inside one variable called fruits.

Arrays are useful when working with:

  • lists of items

  • student marks

  • tasks in a to-do list

  • product collections

They help keep data organized and easier to manage.

2. How to Create an Array

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

Inside the brackets, values are separated by commas.

Example:

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

You can also store numbers in an array.

Example:

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

Or even a list of tasks:

let tasks = ["Study", "Exercise", "Read book"];

Each value inside an array is called an element.

3. Accessing Elements Using Index

Every element in an array has a position number, called an index.

Important: Array indexing starts from 0.

Example array:

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

Index positions:

index value
0 Apple
1 Mango
2 Banana

To access an element, use its index.

Example:

console.log(fruits[0]); 

Output:

Apple

Another example:

console.log(fruits[2]);

Output:

Banana

4. Updating Elements

You can also change values inside an array.

Example:

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

fruits[1] = "Orange";

Now the array becomes:

["Apple", "Orange", "Banana"]

Here we replaced Mango with Orange.

Updating elements is helpful when data changes over time.

5. Array Length Property

JavaScript arrays have a built-in property called length.

It tells you how many elements are inside the array.

Example:

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

console.log(fruits.length);

Output:

4

This is useful when working with loops or checking how many items exist in a list.

6. Basic Looping Over Arrays

Often you want to go through every item in an array.

This can be done using a loop.

Example using a for loop:

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

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

Output:

Apple
Mango
Banana

Here:

  • i represents the index number

  • The loop runs until it reaches the array length

This allows you to process every element in the array.

Why Arrays Are Useful

Arrays help you:

  • store multiple values in one place

  • organize data in order

  • easily access items using indexes

  • loop through collections of data

They are commonly used in things like:

  • product lists in websites

  • user data

  • to-do applications

  • game scores