Arrays in JavaScript: Storing Multiple Values Efficiently

Published: (March 15, 2026 at 01:42 AM EDT)
3 min read
Source: Dev.to

Source: Dev.to

What Are Arrays and Why Do We Need Them?

An array is a data structure used to store a collection of values in a specific order.

Instead of creating many variables like this:

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

We can store them in a single array:

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

Benefits of using arrays

  • Store multiple related values in one place
  • Easier to manage and organize data
  • Simplifies looping through data
  • Makes code cleaner and more scalable

How to Create an Array

Arrays are created using square brackets [].

Example

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

Another example with numbers

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

Each value inside the array is called an element.

Accessing Elements Using Index

Every element in an array has a position, called its index.
Array indexing starts from 0, not 1.

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

Accessing elements

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

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

The number inside the brackets tells JavaScript which position to retrieve.

Updating Elements

Array elements can be updated by assigning a new value to a specific index.

let fruits = ["Apple", "Banana", "Mango"];
fruits[1] = "Orange";
console.log(fruits);

Output

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

Array Length Property

The length property tells us how many elements are in the array.

let fruits = ["Apple", "Banana", "Mango", "Orange"];
console.log(fruits.length);

Output

4

The length property is also useful when working with loops.

Basic Looping Over Arrays

Often we want to process every element in an array. The easiest way to do this is using a for loop.

let fruits = ["Apple", "Banana", "Mango"];
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}

Output

Apple
Banana
Mango

Assignment

Questions

  1. Create an array containing 5 of your favorite movies.
  2. Print the first element and last element of the array.
  3. Change one value in the array and print the updated array.
  4. Loop through the array and print all elements.

Answers

1. Create an Array of Movies

let movies = ["Inception", "Interstellar", "Spirited Away", "The Matrix", "Avengers"];

2. Print the First and Last Element

let movies = ["Inception", "Interstellar", "Spirited Away", "The Matrix", "Avengers"];
console.log(movies[0]);                 // first movie
console.log(movies[movies.length - 1]); // last movie

3. Change One Value

let movies = ["Inception", "Interstellar", "Spirited Away", "The Matrix", "Avengers"];
movies[2] = "Your Name";
console.log(movies);

4. Loop Through the Array

let movies = ["Inception", "Interstellar", "Spirited Away", "The Matrix", "Avengers"];
for (let i = 0; i < movies.length; i++) {
  console.log(movies[i]);
}

Conclusion

Arrays are an essential part of JavaScript because they allow us to store and manage collections of data efficiently. By understanding how to create arrays, access elements, update values, and loop through them, you build a strong foundation for working with larger datasets in real‑world applications.

0 views
Back to Blog

Related posts

Read more »

Modern JS: import and export

!Cover image for Modern JS: import and exporthttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fbmf-tech...

Modern JS Talk: Destructuring Assignment

!Cover image for Modern JS Talk: Destructuring Assignmenthttps://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2...