Array
An Array is a collection of multiple values stored in a single variable.
let fruits = ["Apple", "Mango", "Orange"];
Here, fruits contains three values.
Why Do We Need Arrays?
Without an array, you would write:
let fruit1 = "Apple";
let fruit2 = "Mango";
let fruit3 = "Orange";
Using an array:
let fruits = ["Apple", "Mango", "Orange"];
This makes the code shorter and easier to manage.
Array Index
Each value in an array has an index. The index always starts from 0.
Index: 0 1 2
-------------------------
Array: Apple Mango Orange
Accessing Array Elements
Use the index number to access a value.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits[0]);
console.log(fruits[1]);
// Output:
Apple
Mango
Changing an Array Element
You can update any value using its index.
let fruits = ["Apple", "Mango", "Orange"];
fruits[1] = "Banana";
console.log(fruits);
// Output:
["Apple", "Banana", "Orange"]
Finding the Length of an Array
Use the "length" property.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits.length);
// Output: 3
Adding Elements
- push() – Add at the End
let fruits = ["Apple", "Mango"];
fruits.push("Orange");
console.log(fruits);
// Output: ["Apple", "Mango", "Orange"]
- unshift() – Add at the Beginning
let fruits = ["Mango", "Orange"];
fruits.unshift("Apple");
console.log(fruits);
// Output: ["Apple", "Mango", "Orange"]
Removing Elements
- pop() – Remove from the End
let fruits = ["Apple", "Mango", "Orange"];
fruits.pop();
console.log(fruits);
// Output: ["Apple", "Mango"]
- shift() – Remove from the Beginning
let fruits = ["Apple", "Mango", "Orange"];
fruits.shift();
console.log(fruits);
// Output: ["Mango", "Orange"]
Looping Through an Array
Use a "for loop" to print all elements.
let fruits = ["Apple", "Mango", "Orange"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
// Output:
Apple
Mango
Orange
"for...of" is an easier way to loop through an array.
let fruits = ["Apple", "Mango", "Orange"];
for (let fruit of fruits) {
console.log(fruit);
}
// Output:
Apple
Mango
Orange
Common Array Methods
- includes() - Checks whether an element exists.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits.includes("Mango"));
// Output: true
- indexOf() - Returns the index of an element.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits.indexOf("Orange"));
// Output: 2
- join() - Joins all elements into a single string.
let fruits = ["Apple", "Mango", "Orange"];
console.log(fruits.join(", "));
// Output: Apple, Mango, Orange
- reverse() - Reverses the array.
let fruits = ["Apple", "Mango", "Orange"];
fruits.reverse();
console.log(fruits);
// Output: ["Orange", "Mango", "Apple"]
- sort() - Sorts the array.
let fruits = ["Orange", "Apple", "Mango"];
fruits.sort();
console.log(fruits);
// Output: ["Apple", "Mango", "Orange"]
Array with Different Data Types
An array can store different types of values.
let data = ["Pranay", 21, true, 95.5];
console.log(data);
Nested Arrays
An array can also contain another array.
let numbers = [
[1, 2],
[3, 4]
];
console.log(numbers[1][0]);
// Output: 3
Top comments (0)