Javascript
Mamta Kumawat  

Learn JavaScript arrays with detailed explanations and practical examples of array operations like push, pop, shift, unshift, map, filter, and more.

What is an Array in JavaScript?

An array in JavaScript is a special type of object used to store multiple values in a single variable. Arrays can store any type of data, including numbers, strings, objects, and even other arrays. They are ordered by indices, starting at 0.

Basic Operations on Arrays in JavaScript

There are several key operations you can perform on arrays in JavaScript, such as adding, removing, accessing, and modifying elements. Let’s walk through these operations with detailed explanations and examples.

1. Creating an Array

You can create an array using square brackets [] and separating elements with commas.

let fruits = ["Apple", "Banana", "Cherry", "Date"];
console.log(fruits);  // Output: ["Apple", "Banana", "Cherry", "Date"]

2. Accessing Array Elements

You can access an array element by using its index, with the first element having index 0.

console.log(fruits[0]);  // Output: Apple (first element)
console.log(fruits[2]);  // Output: Cherry (third element)

3. Modifying Array Elements

You can modify an array element by directly assigning a new value to a specific index.

fruits[1] = "Blueberry";  // Change "Banana" to "Blueberry"
console.log(fruits);  // Output: ["Apple", "Blueberry", "Cherry", "Date"]

4. Adding Elements

You can add new elements to an array using different methods.

  • Using .push(): Adds an element to the end of the array.
fruits.push("Elderberry");  // Adds "Elderberry" to the end
console.log(fruits);  // Output: ["Apple", "Blueberry", "Cherry", "Date", "Elderberry"]
  • Using .unshift(): Adds an element to the beginning of the array.
fruits.unshift("Acai");  // Adds "Acai" to the beginning
console.log(fruits);  // Output: ["Acai", "Apple", "Blueberry", "Cherry", "Date", "Elderberry"]

5. Removing Elements

You can remove elements from an array using the following methods:

  • Using .pop(): Removes the last element from the array.
let removedFruit = fruits.pop();  // Removes "Elderberry" from the end
console.log(removedFruit);  // Output: Elderberry
console.log(fruits);  // Output: ["Acai", "Apple", "Blueberry", "Cherry", "Date"]
  • Using .shift(): Removes the first element from the array.
let shiftedFruit = fruits.shift();  // Removes "Acai" from the beginning
console.log(shiftedFruit);  // Output: Acai
console.log(fruits);  // Output: ["Apple", "Blueberry", "Cherry", "Date"]
  • Using .splice(): Removes elements from any position in the array and can also be used to add elements.
fruits.splice(1, 1);  // Removes 1 element starting from index 1 (removes "Blueberry")
console.log(fruits);  // Output: ["Apple", "Cherry", "Date"]

6. Finding the Length of an Array

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

console.log(fruits.length);  // Output: 3 (since the array has 3 elements)

7. Searching for Elements

You can use several methods to find or check for elements in an array.

  • Using .indexOf(): Returns the index of the first occurrence of an element or -1 if not found.
console.log(fruits.indexOf("Cherry"));  // Output: 1 (index of "Cherry")
console.log(fruits.indexOf("Apple"));  // Output: 0 (index of "Apple")
console.log(fruits.indexOf("Grapes"));  // Output: -1 (not found)
  • Using .includes(): Returns true if an element exists in the array, false otherwise.
console.log(fruits.includes("Apple"));  // Output: true
console.log(fruits.includes("Grapes"));  // Output: false

8. Looping Through Arrays

You can use loops to iterate over the elements in an array.

  • Using for loop:
for (let i = 0; i < fruits.length; i++) {
  console.log(fruits[i]);
}
// Output:
// Apple
// Cherry
// Date
  • Using .forEach(): A modern way to loop through an array.
fruits.forEach(function(fruit) {
  console.log(fruit);
});
// Output:
// Apple
// Cherry
// Date

9. Transforming Arrays

JavaScript provides several methods to transform or create new arrays.

  • Using .map(): Creates a new array with the results of calling a provided function on every element in the array.
let upperFruits = fruits.map(function(fruit) {
  return fruit.toUpperCase();  // Convert each fruit name to uppercase
});
console.log(upperFruits);  // Output: ["APPLE", "CHERRY", "DATE"]
  • Using .filter(): Creates a new array with all elements that pass a test.
let longNames = fruits.filter(function(fruit) {
  return fruit.length > 5;  // Only fruits with names longer than 5 characters
});
console.log(longNames);  // Output: ["Cherry"]
  • Using .reduce(): Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
let totalLength = fruits.reduce(function(accumulator, fruit) {
  return accumulator + fruit.length;  // Adds the length of each fruit name
}, 0);
console.log(totalLength);  // Output: 15 (Apple=5, Cherry=6, Date=4)

10. Joining Array Elements into a String

The .join() method joins all the elements of an array into a single string, with a specified separator.

let fruitString = fruits.join(", ");
console.log(fruitString);  // Output: "Apple, Cherry, Date"

Summary of Key Array Operations:

  1. Creating arrays: let fruits = ["Apple", "Banana", "Cherry"];
  2. Accessing elements: console.log(fruits[0]);
  3. Modifying elements: fruits[1] = "Blueberry";
  4. Adding elements: .push(), .unshift()
  5. Removing elements: .pop(), .shift(), .splice()
  6. Finding length: fruits.length
  7. Searching: .indexOf(), .includes()
  8. Looping: for, .forEach()
  9. Transforming: .map(), .filter(), .reduce()
  10. Joining: .join()

Leave A Comment