1. JavaScript Arrays
In JavaScript, arrays are used to store multiple values in a single variable. Arrays are one of the most widely used data structures and are essential for handling collections of data.
2. Key Features of Arrays:
Declaring an Array
You can create an array using either of the following methods:
1. Using Array Literals:
const fruits = ["Apple", "Banana", "Orange"];
2. Using Array Literals:
const fruits = new Array("Apple", "Banana", "Orange");
Accessing Array Elements
You can access elements by their index.
console.log(fruits[0]); // Output: "Apple"
console.log(fruits[2]); // Output: "Orange"
Modifying Array Elements
You can change the value of an array element by referencing its index.
fruits[1] = "Mango";
console.log(fruits); // Output: ["Apple", "Mango", "Orange"]
Common Array Methods
JavaScript arrays come with a number of built-in methods for performing common operations:
1. Adding Elements
push(): Adds an element to the end of the array.
fruits.push("Grapes");
console.log(fruits); // Output: ["Apple", "Mango", "Orange", "Grapes"]
unshift()
: Adds an element to the beginning of the array.
fruits.unshift("Strawberry");
console.log(fruits); // Output: ["Strawberry", "Apple", "Mango", "Orange", "Grapes"]
2. Removing Elements
pop()
: Removes the last element from the array.
fruits.pop();
console.log(fruits); // Output: ["Strawberry", "Apple", "Mango", "Orange"]
shift()
: Removes the first element from the array.
fruits.shift();
console.log(fruits); // Output: ["Apple", "Mango", "Orange"]
3. Finding the Length of an Array
The
length
property returns the number of elements in an array.
console.log(fruits.length); // Output: 3
4. Concatenating Arrays
You can join two arrays together using the
concat()
method.
const vegetables = ["Carrot", "Tomato"];
const food = fruits.concat(vegetables);
console.log(food); // Output: ["Apple", "Mango", "Orange", "Carrot", "Tomato"]
5. Slicing and Splicing Arrays
slice(start, end): Extracts a section of an array without modifying the original array.
const citrus = fruits.slice(1, 3);
console.log(citrus); // Output: ["Mango", "Orange"]
splice(start, deleteCount, item1, item2, ...): Adds/removes elements from the array. It modifies the original array.
fruits.splice(1, 1, "Pineapple");
console.log(fruits); // Output: ["Apple", "Pineapple", "Orange"]
6. Finding Elements in an Array
indexOf()
: Returns the first index at which a given element can be found, or -1
if the element is not present.
console.log(fruits.indexOf("Orange")); // Output: 2
includes()
: Checks if an element exists in the array.
console.log(fruits.includes("Mango")); // Output: false
7. Sorting Arrays
sort()
: Sorts the elements of an array.
const numbers = [10, 5, 3, 8];
numbers.sort();
console.log(numbers); // Output: [10, 3, 5, 8] (Incorrect for numbers, as it sorts alphabetically)
For correct numerical sorting, you need to pass a comparison function:
numbers.sort((a, b) => a - b);
console.log(numbers); // Output: [3, 5, 8, 10]
reverse(): Reverses the order of the elements in an array.
numbers.reverse();
console.log(numbers); // Output: [10, 8, 5, 3]
Iterating Over Arrays
1. for Loop
You can use a for loop to iterate through an array.
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
2. forEach() Method
This method executes a provided function once for each array element.
3. map() Method
The map() method creates a new array with the results of calling a provided function on every element in the array.
const fruitLengths = fruits.map(fruit => fruit.length);
console.log(fruitLengths); // Output: [5, 9, 6]
Multi-dimensional Arrays
JavaScript also supports arrays within arrays, allowing you to create multi-dimensional arrays:
const matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
console.log(matrix[1][2]); // Output: 6
Conclusion
JavaScript arrays provide an efficient way to manage and manipulate collections of data. With the various built-in methods and properties, you can easily add, remove, and modify elements, as well as iterate over arrays for complex operations. Arrays are a fundamental tool for any developer, and mastering them is crucial for working effectively with JavaScript.