Skip to main content

JavaScript Arrays Explained

Updated 4 min readMarkdown

In many programs, we need to store multiple values together.

For example, imagine storing a list of fruits:

  • Apple

  • Banana

  • Mango

  • Orange

If we store them individually, it becomes inefficient.

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

This approach quickly becomes messy when the list grows.

JavaScript solves this problem using arrays.


What is an Array?

An array is a collection of values stored in order.

Example:

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

Here, all the fruit names are stored inside a single variable called fruits.


Why Do We Need Arrays?

Arrays help us:

  • store multiple values in one variable

  • organize data in order

  • easily access and update values

  • loop through lists of items

Example use cases:

  • list of tasks

  • list of marks

  • list of products

  • list of messages


Visual Representation of an Array

https://www.cs.emory.edu/~cheung/Courses/170/Syllabus/09/FIGS/array02b.gif https://www.educative.io/api/page/5315705890144256/image/download/6142425421578240 https://miro.medium.com/1%2A7ZPlXgiqnWXJbUrDnz3gCA.png

4

Each element in an array has an index.

Important rule:

Indexing always starts from 0.

Example:

Index Value
0 Apple
1 Banana
2 Mango
3 Orange

Creating an Array

Arrays are created using square brackets [].

Example:

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

Another example:

let tasks = ["Study", "Exercise", "Coding", "Reading"];

Accessing Elements Using Index

To access elements, we use the index number.

Example:

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

console.log(fruits[0]);

Output

Apple

Another example:

console.log(fruits[2]);

Output

Mango

Remember:

Index starts from 0, not 1.


Updating Elements in an Array

Array elements can be updated using their index.

Example:

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

fruits[1] = "Grapes";

console.log(fruits);

Output

["Apple", "Grapes", "Mango"]

The value "Banana" was replaced by "Grapes".


Array Length Property

Arrays provide a useful property called length.

It tells us the number of elements in the array.

Example:

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

console.log(fruits.length);

Output

4

This is useful when working with loops.


Looping Through Arrays

To process all elements in an array, we can use a loop.

Example:

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

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

Output

Apple
Banana
Mango

How this works:

  1. i starts at 0

  2. The loop continues while i < fruits.length

  3. Each iteration prints one element


Storing Values Individually vs Using an Array

Without arrays:

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

With arrays:

let marks = [80, 85, 90];

Arrays make programs cleaner and easier to manage.


Practical Example

let marks = [75, 80, 90];

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

Output

75
80
90

Practice Assignment

Create an Array of Favorite Movies

Create an array with 5 movie names.

Example:

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

console.log(movies[0]);
console.log(movies[4]);

Change One Value

Update one movie name.

Example:

movies[2] = "The Dark Knight";

Print the updated array.

console.log(movies);

Loop Through the Array

Use a loop to print all movies.

Example:

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

No comments yet. Be the first to comment.