Skip to main content

Command Palette

Search for a command to run...

Arrays 101

Updated
4 min readView as Markdown
Arrays 101

Introduction

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

For example, imagine storing your favorite fruits:

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

This works, but it quickly becomes messy when the list grows.

Instead of storing values in separate variables, we can use an array.

Arrays allow us to store multiple values in a single variable.


What Are Arrays?

An array is a collection of values stored in order.

Example:

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

Here we have one variable called fruits that contains multiple values.

You can imagine an array like a list:

fruits
[ "Apple", "Banana", "Mango", "Orange" ]

Each item in the array has a position called an index.

Important rule:

Array indexing starts from 0, not 1.

So the positions look like this:

Index →   0        1        2        3
Value → "Apple" "Banana" "Mango" "Orange"


Arrays in JavaScript

Arrays in JavaScript are more flexible than arrays in many other programming languages.

In languages like Java or C++, arrays usually store only one type of data.

Example in Java:

int[] numbers = {10, 20, 30};

This array can only store integers.


But JavaScript is different.

JavaScript arrays can store multiple data types together.

Example:

let mixedArray = [10, "Hello", true, null];

This array contains:

Value Type
10 Number
"Hello" String
true Boolean
null Null

This flexibility is one reason why JavaScript arrays are very powerful.


Why Do We Need Arrays?

Imagine storing marks of students.

Without arrays:

let mark1 = 85;
let mark2 = 90;
let mark3 = 78;
let mark4 = 92;

With arrays:

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

Now everything is organized in one variable.

Arrays make programs:

  • Cleaner

  • Easier to manage

  • Easier to loop through


How to Create an Array

Arrays are created using square brackets [].

Example:

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

Another example:

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

Accessing Elements Using Index

To access an element in an array, we use its index.

Example:

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

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

Output:

Apple
Banana

Remember:

First element index → 0
Second element index → 1
Third element index → 2

Updating Array Elements

Array values can be modified using their index.

Example:

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

fruits[1] = "Orange";

console.log(fruits);

Output:

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

We changed "Banana" to "Orange".


Array Length Property

JavaScript arrays have a property called length.

It tells us how many elements are inside the array.

Example:

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

console.log(fruits.length);

Output:

4

This is useful when looping through arrays.


Looping Over Arrays

We often need to print or process 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

Explanation:

  • Start from index 0

  • Continue until the array length

  • Print each element



Assignment Practice

Try implementing the following.

Step 1: Create an Array of Favorite Movies

let movies = ["Inception", "Interstellar", "Avengers", "Batman", "Titanic"];

Step 2: Print First and Last Element

console.log(movies[0]);

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

Step 3: Update One Value

movies[2] = "Spider-Man";

Step 4: Loop Through the Array

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

Conclusion

Arrays are one of the most important structures in JavaScript.

They allow us to store multiple values in a single variable and access them using indexes.

In this article we learned:

  • What arrays are

  • Why arrays are useful

  • How JavaScript arrays differ from other languages

  • How to create arrays

  • How to access and update elements

  • The length property

  • How to loop through arrays

Understanding arrays is essential because they are used everywhere in JavaScript, including:

  • Lists of users

  • Product catalogs

  • API responses

  • Data processing