Arrays in JavaScript

Javascript 8 min min read Updated: Mar 09, 2026 Beginner
Arrays in JavaScript
Beginner Topic 9 of 15

Arrays in JavaScript

In JavaScript, an array is a special type of variable used to store multiple values in a single variable. Instead of creating separate variables for each value, arrays allow developers to group related data together.

For example, if you want to store a list of fruits such as Apple, Mango, and Banana, using an array is much easier than creating multiple variables.

What is an Array?

An array is a collection of elements stored in a single variable. Each value inside an array is called an element, and each element has a position called an index.

Array indexing starts from 0. This means the first element is at index 0, the second element is at index 1, and so on.

Creating an Array

Arrays in JavaScript can be created using square brackets [].

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

Output:

output ["Apple", "Mango", "Banana"]

Accessing Array Elements

Array elements can be accessed using their index number.

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

Output:

output Apple Mango

Changing Array Elements

You can modify elements in an array by assigning a new value to a specific index.

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

Output:

output ["Apple", "Orange", "Banana"]

Array Length

The length property is used to find the number of elements in an array.

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

Output:

output 3

Adding Elements to an Array

JavaScript provides methods to add new elements to an array.

Using push()

The push() method adds a new element to the end of an array.

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

Using unshift()

The unshift() method adds a new element at the beginning of an array.

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

Removing Elements from an Array

JavaScript also provides methods to remove elements from arrays.

Using pop()

The pop() method removes the last element from an array.

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

Using shift()

The shift() method removes the first element from an array.

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

Looping Through Arrays

Arrays are often used with loops to access each element one by one.

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

Why Arrays are Important

Arrays are widely used in JavaScript applications because they allow developers to manage collections of data efficiently. They are commonly used for storing lists of products, user data, messages, and many other types of information.

Using arrays helps keep programs organized and reduces the need to create many separate variables.

Conclusion

Arrays are powerful data structures in JavaScript that allow developers to store multiple values in a single variable. They support many useful methods for adding, removing, and accessing elements.

Understanding arrays is essential for working with data in JavaScript applications. In the next tutorial, you will learn about Objects in JavaScript, which allow developers to store more complex data structures.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators