Skip to main content

Command Palette

Search for a command to run...

JavaScript Arrays 101

Updated
4 min readView as Markdown

When we begin learning programming, we quickly realize that managing multiple pieces of data efficiently is essential. Storing individual values in separate variables can become messy and difficult to maintain as the amount of data grows. This is where arrays come into play. Arrays provide a simple and powerful way for us to organize and manage collections of data in our programs.

What Are Arrays and Why We Need Them

An array is a data structure that allows us to store multiple values inside a single variable. Instead of creating many variables to hold related data, we can store all those values in one array.

For example, imagine storing the marks of five students. Without arrays, we might create five separate variables:

marks1 = 85
marks2 = 90
marks3 = 78
marks4 = 92
marks5 = 88

This approach becomes difficult to manage when the number of values increases. Arrays solve this problem by allowing us to store all the values together:

marks = [85, 90, 78, 92, 88]

Now we have a clean and organized structure that makes handling multiple values much easier.

How to Create an Array

Creating an array is simple in most programming languages. In JavaScript, for example, we create an array using square brackets [].

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

Here, fruits is an array that stores four values. Each value inside the array is called an element.

Arrays can store numbers, strings, or even other data types depending on the language.

Accessing Elements Using Index

Each element in an array has a specific position called an index. Most programming languages start counting from 0, not 1.

For example:

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

The indexes look like this:

0 → Apple  
1 → Banana  
2 → Mango  
3 → Orange

To access a specific element, we use its index:

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

Using indexes allows us to retrieve any element quickly.

Updating Elements in an Array

Arrays are not fixed; we can modify their values whenever needed. To update an element, we simply assign a new value to the desired index.

Example:

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

fruits[1] = "Pineapple";

Now the array becomes:

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

This flexibility allows us to manage changing data easily.

The Array Length Property

Most programming languages provide a way to determine how many elements exist inside an array. In JavaScript, we use the length property.

Example:

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

console.log(fruits.length);

The output will be:

4

Knowing the length of an array helps us when working with loops or when checking how much data we have stored.

Basic Looping Over Arrays

Often, we need to go through every element in an array to perform some action. This process is called looping.

One common way to loop through an array is using a for loop:

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

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

In this loop:

  • i starts from 0

  • The loop continues until it reaches the length of the array

  • Each iteration prints the next element

This technique is extremely useful when working with lists of data.

Conclusion

Arrays are one of the most fundamental data structures in programming. They allow us to store multiple values in a single variable, access elements using indexes, update values easily, and process large sets of data through loops. As we continue building programs, arrays become an essential tool that helps keep our code organized and efficient.