Skip to main content

Command Palette

Search for a command to run...

JavaScriptt Arrays 101

Updated
6 min read
JavaScriptt  Arrays 101

Introduction

In programming, we often need to store multiple values together.

Examples:

  • List of fruits

  • Student marks

  • Todo tasks

  • Favorite movies

Without arrays, we would need many separate variables.

Example:

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

This quickly becomes difficult to manage.

To solve this problem, JavaScript provides:

Arrays

Arrays help us store multiple values in a single structure.

In this article, we will learn:

  • What arrays are

  • Why arrays are needed

  • Creating arrays

  • Accessing elements

  • Updating values

  • Array length

  • Looping through arrays


What Is an Array?

An array is:

A collection of values stored in order.

Example:

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

Here:

  • "Apple" is the first value

  • "Banana" is the second value

  • "Mango" is the third value

All values are stored together inside one array.


Real-Life Analogy

Think of an array like a train.

[Apple] [Banana] [Mango]

Each compartment stores one value.

And every compartment has a position number called:

Index

Why Arrays Are Needed

Without arrays:

let mark1 = 90;
let mark2 = 85;
let mark3 = 95;

Managing many variables becomes messy.

With arrays:

const marks = [90, 85, 95];

Cleaner and easier to manage.


Creating an Array

Arrays are created using:

[]

called:

Square Brackets

Simple Array Example

const colors = ["Red", "Blue", "Green"];

console.log(colors);

Output:

[ 'Red', 'Blue', 'Green' ]

Arrays Can Store Different Data Types

const data = ["Ranjan", 21, true];

console.log(data);

Output:

[ 'Ranjan', 21, true ]

Understanding Array Index

Every array element has a position.

Important rule:

Array indexing starts from 0.


Visual Representation of Array Index and Values

Index:   0        1         2
       ┌──────┬────────┬───────┐
Value: │Apple │Banana  │Mango  │
       └──────┴────────┴───────┘

Accessing Array Elements

We access elements using:

array[index]

Example

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

console.log(fruits[0]);

Output:

Apple

Accessing More Elements

console.log(fruits[1]);

console.log(fruits[2]);

Output:

Banana
Mango

Accessing Invalid Index

console.log(fruits[10]);

Output:

undefined

Because index 10 does not exist.


Updating Array Elements

Array values can be changed using indexes.

Example:

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

fruits[1] = "Orange";

console.log(fruits);

Output:

[ 'Apple', 'Orange', 'Mango' ]

Memory-Style Block Diagram for Array Storage

       ┌──────┬────────┬───────┐
Index: │  0   │   1    │   2   │
       ├──────┼────────┼───────┤
Value: │Apple │Orange  │Mango  │
       └──────┴────────┴───────┘

Array Length Property

JavaScript arrays have a built-in property:

length

which tells how many elements exist.


Example

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

console.log(fruits.length);

Output:

3

Why length Is Useful

Useful for:

  • Counting items

  • Loops

  • Dynamic operations


Basic Looping Over Arrays

Looping means:

Accessing all array elements one by one.


Using for Loop

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

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

Output:

Apple
Banana
Mango

Understanding the Loop

Step-by-step:

i = 0 → fruits[0]
i = 1 → fruits[1]
i = 2 → fruits[2]

Loop continues until:

i < fruits.length

becomes false.


Array Traversal Visualization

fruits array
     │
     ▼
[Apple] → [Banana] → [Mango]
     │         │         │
     ▼         ▼         ▼
 print     print     print

Real-World Example

const tasks = [
  "Learn JavaScript",
  "Practice Coding",
  "Build Projects",
];

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

Output:

Learn JavaScript
Practice Coding
Build Projects

Difference Between Variables and Arrays

Variables Arrays
Store single value Store multiple values
Many variables needed One structure handles all
Hard to manage Easier organization

Common Beginner Mistakes

1. Forgetting Index Starts at 0

Wrong expectation:

fruits[1] → Apple

Correct:

fruits[0] → Apple

2. Accessing Invalid Index

fruits[100]

returns:

undefined

3. Confusing Length with Last Index

Example:

fruits.length

is:

3

But last index is:

2

Best Practices

1. Use Meaningful Array Names

Good:

students
movies
tasks

Bad:

x
y
z

2. Keep Arrays Organized

Store related values together.


3. Use Loops Instead of Repetition

Avoid:

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

Use loops instead.


Assignment

Create an Array of 5 Favorite Movies

const movies = [
  "Inception",
  "Interstellar",
  "Avengers",
  "Batman",
  "Joker",
];

Print First and Last Element

console.log(movies[0]);

console.log(movies[movies.length - 1]);

Output:

Inception
Joker

Change One Value

movies[2] = "Spider-Man";

console.log(movies);

Loop Through Array

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

Mental Model

Think of arrays like numbered lockers.

Locker 0 → Apple
Locker 1 → Banana
Locker 2 → Mango

Indexes help locate values quickly.


Full Working Example

const movies = [
  "Inception",
  "Interstellar",
  "Avengers",
  "Batman",
  "Joker",
];

console.log("First Movie:");
console.log(movies[0]);

console.log("Last Movie:");
console.log(movies[movies.length - 1]);

movies[2] = "Spider-Man";

console.log("Updated Array:");
console.log(movies);

console.log("All Movies:");

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

Output:

First Movie:
Inception

Last Movie:
Joker

Updated Array:
[ 'Inception', 'Interstellar', 'Spider-Man', 'Batman', 'Joker' ]

All Movies:
Inception
Interstellar
Spider-Man
Batman
Joker

Conclusion

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

Key takeaways:

  • Arrays store multiple values together

  • Arrays maintain order

  • Indexing starts from 0

  • Elements can be updated using indexes

  • length gives total number of elements

  • Loops help process arrays efficiently

Understanding arrays is essential because almost every JavaScript application uses them heavily.

1 views