DEV Community

Adhi sankar
Adhi sankar

Posted on

Array in JavaScript

Introduction

An array is one of the most useful data structures in JavaScript. It allows us to store multiple values in a single variable instead of creating separate variables for each value. Arrays make it easier to organize, access, and manipulate collections of data.

What is an Array?

An array is a special type of object that stores multiple values in an ordered list. Each value in an array is called an element, and every element has an index. The index always starts from 0.

Syntax

let fruits = ["Apple", "Banana", "Mango"];
Enter fullscreen mode Exit fullscreen mode

In the above example:

  • Apple is at index 0
  • Banana is at index 1
  • Mango is at index 2

Accessing Array Elements

You can access any element by using its index.

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

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

Updating an Array Element

You can change an existing element by assigning a new value.

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

fruits[1] = "Orange";

console.log(fruits);
Enter fullscreen mode Exit fullscreen mode

Output:

["Apple", "Orange", "Mango"]
Enter fullscreen mode Exit fullscreen mode

Finding the Length of an Array

The length property returns the total number of elements in an array.

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

console.log(numbers.length);
Enter fullscreen mode Exit fullscreen mode

Output:

4
Enter fullscreen mode Exit fullscreen mode

Common Array Methods

1. push()

Adds an element to the end of the array.

let colors = ["Red", "Blue"];
colors.push("Green");

console.log(colors);
Enter fullscreen mode Exit fullscreen mode

2. pop()

Removes the last element.

let colors = ["Red", "Blue", "Green"];
colors.pop();

console.log(colors);
Enter fullscreen mode Exit fullscreen mode

3. unshift()

Adds an element to the beginning.

let colors = ["Blue", "Green"];
colors.unshift("Red");

console.log(colors);
Enter fullscreen mode Exit fullscreen mode

4. shift()

Removes the first element.

let colors = ["Red", "Blue", "Green"];
colors.shift();

console.log(colors);
Enter fullscreen mode Exit fullscreen mode

5. indexOf()

Returns the index of an element.

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

console.log(fruits.indexOf("Banana"));
Enter fullscreen mode Exit fullscreen mode

6. includes()

Checks whether an element exists in the array.

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

console.log(fruits.includes("Mango"));
Enter fullscreen mode Exit fullscreen mode

Looping Through an Array

A for loop is commonly used to access every element.

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

for (let i = 0; i < numbers.length; i++) {
    console.log(numbers[i]);
}
Enter fullscreen mode Exit fullscreen mode

Real-Life Example

Suppose a school wants to store student names. Instead of creating many variables, we can use an array.

let students = ["Rahul", "Priya", "John"];

students.push("Anu");

for (let i = 0; i < students.length; i++) {
    console.log(students[i]);
}
Enter fullscreen mode Exit fullscreen mode

Advantages of Arrays

  • Store multiple values in a single variable.
  • Easy to access using indexes.
  • Reduce code repetition.
  • Support many built-in methods for data manipulation.
  • Improve code readability and efficiency.

Conclusion

Arrays are an essential part of JavaScript programming. They help developers store, organize, and manipulate collections of data efficiently. By learning arrays and their methods, you can solve many programming problems more easily and write cleaner, more efficient code.

Top comments (0)