Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
•4 min read•View as Markdown
JavaScript Arrays 101
J

I'm Janmejai Singh, a Full Stack Developer specializing in the MERN stack, on a mission to blend technical excellence with creative flair. From machine learning and blockchain breakthroughs to game development, 3D design, and animation adventures, I bring humor and heart to every line of code. Join me as I turn innovative ideas into digital masterpieces!

When learning programming, we often deal with multiple pieces of similar data. For example:

  • A list of fruits

  • A list of student marks

  • A list of daily tasks

  • A list of favorite movies

Instead of storing each value in a separate variable, arrays help us store multiple values together in an organized way.

Let’s understand this step by step.


1. What Arrays Are and Why We Need Them

Imagine you want to store the names of five fruits.

Without arrays, you might do something like this:

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

This approach becomes messy when the number of items increases.

Instead, we can store all values in one variable using an array.

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

Why arrays are useful

  • Store multiple values in one variable

  • Keep data organized

  • Easy to access and update values

  • Helpful when working with loops


2. How to Create an Array

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

Example:

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

You can also store different types of values:

let mixedData = ["Jai", 24, true];

But usually arrays contain similar types of data.

Example:

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

3. Accessing Elements Using Index

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

Important rule:

Array indexing starts from 0.

Example:

let fruits = ["Apple", "Banana", "Mango"];
Index Value
0 Apple
1 Banana
2 Mango

Access values like this:

console.log(fruits[0]); // Apple
console.log(fruits[1]); // Banana
console.log(fruits[2]); // Mango

4. Updating Elements in an Array

You can change any element by using its index.

Example:

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

fruits[1] = "Pineapple";

console.log(fruits);

Output:

["Apple", "Pineapple", "Mango"]

5. Array Length Property

Arrays have a built-in 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 very useful when working with loops.


6. Basic Looping Over Arrays

Loops help us access every element in an array.

Example using a for loop:

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

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

Output:

Apple
Banana
Mango
Orange

The loop runs from index 0 to the last element.


7. Assignment Example

Let’s solve the assignment step by step.

Step 1: Create an array of 5 favorite movies

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

Step 2: Print the first and last movie

console.log("First movie:", movies[0]);
console.log("Last movie:", movies[movies.length - 1]);

Step 3: Change one value

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

Step 4: Loop through the array

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

8. Visual Diagram of Array

Index Representation

Index:   0        1        2        3        4
        ---------------------------------------
Value: | Apple | Banana | Mango | Orange | Grapes |
        ---------------------------------------

Memory-Style Array Representation

+--------+--------+--------+--------+
| Apple  | Banana | Mango  | Orange |
+--------+--------+--------+--------+
   [0]       [1]      [2]      [3]

Each block represents a position in memory storing a value.


Conclusion

Arrays are one of the most important data structures in JavaScript. They allow developers to store and manage multiple values efficiently.

In this article we learned:

  • What arrays are

  • How to create arrays

  • Access elements using index

  • Update values

  • Use the length property

  • Loop through arrays

Mastering arrays is a fundamental step toward becoming a strong JavaScript developer.