Arrays in JavaScript
An array in JavaScript is used to store multiple values in a single variable. Instead of creating separate variables for each value, we can store all the values together inside an array.
For example, if we want to store the names of multiple programming languages, we can use an array.
let languages = ["JavaScript", "Python", "Java"];
console.log(languages);
Output:
["JavaScript", "Python", "Java"]
Here, languages is an array that stores multiple values.
Creating an Array
An array is created using square brackets []. Each value inside an array is called an element, and the elements are separated by commas.
let fruits = ["Apple", "Mango", "Orange"];
In this example:
-
fruitsis the array. -
"Apple","Mango", and"Orange"are elements.
Array Index
Each element in an array has an index number. Array indexing starts from 0.
For example:
let fruits = ["Apple", "Mango", "Orange"];
The index positions are:
| Index | Element |
|---|---|
0 |
"Apple" |
1 |
"Mango" |
2 |
"Orange" |
Accessing Array Elements
We can access an array element by using its index inside square brackets.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits[0]);
console.log(fruits[1]);
Output:
Apple
Mango
Here, fruits[0] accesses the first element, and fruits[1] accesses the second element.
Updating an Array Element
We can update an element by assigning a new value to its index.
let fruits = ["Apple", "Mango", "Orange"];
fruits[1] = "Banana";
console.log(fruits);
Output:
["Apple", "Banana", "Orange"]
Here, the value "Mango" is replaced with "Banana".
Adding Elements to an Array
We can add new elements to an array.
Using push()
The push() method adds one or more elements to the end of an array.
let fruits = ["Apple", "Mango"];
fruits.push("Orange");
console.log(fruits);
Output:
["Apple", "Mango", "Orange"]
Using unshift()
The unshift() method adds one or more elements to the beginning of an array.
let fruits = ["Mango", "Orange"];
fruits.unshift("Apple");
console.log(fruits);
Output:
["Apple", "Mango", "Orange"]
Removing Elements from an Array
We can also remove elements from an array.
Using pop()
The pop() method removes the last element from an array.
let fruits = ["Apple", "Mango", "Orange"];
fruits.pop();
console.log(fruits);
Output:
["Apple", "Mango"]
Here, "Orange" is removed from the array.
Using shift()
The shift() method removes the first element from an array.
let fruits = ["Apple", "Mango", "Orange"];
fruits.shift();
console.log(fruits);
Output:
["Mango", "Orange"]
Here, "Apple" is removed from the array.
Finding the Length of an Array
The length property is used to find the number of elements in an array.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits.length);
Output:
3
The array contains three elements, so the length is 3.
Looping Through an Array
We can use a loop to access each element in an array.
let fruits = ["Apple", "Mango", "Orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
Output:
Apple
Mango
Orange
Here, i starts from 0, which is the first index of the array. The loop continues until it reaches the length of the array.
Arrays Can Store Different Data Types
An array can store values of different data types.
let data = ["John", 25, true, null];
console.log(data);
Here, the array contains a String, Number, Boolean, and Null value.
However, it is generally better to store similar types of data in an array for better readability and organization.
Array of Objects
An array can also store objects.
let students = [
{
name: "John",
age: 20
},
{
name: "David",
age: 22
}
];
console.log(students[0].name);
Output:
John
Here, students is an array containing two objects. students[0] accesses the first object, and .name accesses its name property.
Common Array Methods
| Method | Description |
|---|---|
push() |
Adds an element to the end of an array |
pop() |
Removes the last element |
unshift() |
Adds an element to the beginning |
shift() |
Removes the first element |
length |
Returns the number of elements |
Top comments (0)