What is an Array?

An array is a special variable, which can hold more than one value at a time. It is a collection of elements stored in a single variable.

Creating an Array

There are multiple ways to create an array in JavaScript:

  1. Using an array literal:

      let fruits = ["Apple", "Banana", "Cherry"];
    console.log(fruits); // ["Apple", "Banana", "Cherry"]
      
  2. Using the Array constructor:

      let fruits = new Array("Apple", "Banana", "Cherry");
    console.log(fruits); // ["Apple", "Banana", "Cherry"]
      

Accessing Array Elements

You can access elements of an array by their index:

  let fruits = ["Apple", "Banana", "Cherry"];
console.log(fruits[0]); // "Apple"
console.log(fruits[1]); // "Banana"
console.log(fruits[2]); // "Cherry"
  

Modifying Array Elements

You can modify elements of an array by their index:

  let fruits = ["Apple", "Banana", "Cherry"];
fruits[1] = "Blueberry";
console.log(fruits); // ["Apple", "Blueberry", "Cherry"]
  

Array Methods

  1. push()

    Adds new elements to the end of an array:

      let fruits = ["Apple", "Banana"];
    fruits.push("Cherry");
    console.log(fruits); // ["Apple", "Banana", "Cherry"]
      
  2. pop()

    Removes the last element of an array:

      let fruits = ["Apple", "Banana", "Cherry"];
    let lastFruit = fruits.pop();
    console.log(fruits); // ["Apple", "Banana"]
    console.log(lastFruit); // "Cherry"
      
  3. shift()

    Removes the first element of an array:

      let fruits = ["Apple", "Banana", "Cherry"];
    let firstFruit = fruits.shift();
    console.log(fruits); // ["Banana", "Cherry"]
    console.log(firstFruit); // "Apple"
      
  4. unshift()

    Adds new elements to the beginning of an array:

      let fruits = ["Banana", "Cherry"];
    fruits.unshift("Apple");
    console.log(fruits); // ["Apple", "Banana", "Cherry"]
      
  5. length

    Returns the length of an array:

      let fruits = ["Apple", "Banana", "Cherry"];
    console.log(fruits.length); // 3
      
  6. concat()

    Joins two or more arrays:

      let fruits = ["Apple", "Banana"];
    let moreFruits = ["Cherry", "Date"];
    let allFruits = fruits.concat(moreFruits);
    console.log(allFruits); // ["Apple", "Banana", "Cherry", "Date"]
      
  7. slice()

    Returns selected elements in an array, as a new array:

      let fruits = ["Apple", "Banana", "Cherry", "Date"];
    let someFruits = fruits.slice(1, 3);
    console.log(someFruits); // ["Banana", "Cherry"]
      
  8. splice()

    Adds/removes elements from an array:

      let fruits = ["Apple", "Banana", "Cherry"];
    fruits.splice(1, 1, "Blueberry", "Kiwi");
    console.log(fruits); // ["Apple", "Blueberry", "Kiwi", "Cherry"]
      
  9. forEach()

    Calls a function once for each array element:

      let fruits = ["Apple", "Banana", "Cherry"];
    fruits.forEach(function(fruit) {
    console.log(fruit);
    });
    // Output:
    // "Apple"
    // "Banana"
    // "Cherry"
      
  10. filter()

    Creates a new array with elements that pass a test:

      let numbers = [1, 2, 3, 4, 5];
    let evens = numbers.filter(n => n % 2 === 0);
    console.log(evens); // [2, 4]
      
  11. reduce()

    Reduces an array to a single value:

      let numbers = [1, 2, 3, 4];
    let sum = numbers.reduce((acc, curr) => acc + curr, 0);
    console.log(sum); // 10
      
  12. find() and findIndex()

    Returns the first element or index that matches a condition:

      let users = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
    console.log(users.find(u => u.id === 2));      // { id: 2, name: 'Bob' }
    console.log(users.findIndex(u => u.id === 2)); // 1
      
  13. includes()

    Checks whether an array contains a value:

      let fruits = ['Apple', 'Banana'];
    console.log(fruits.includes('Banana')); // true
      
  14. sort() and reverse()

      let nums = [3, 1, 4, 1, 5];
    nums.sort((a, b) => a - b);
    console.log(nums); // [1, 1, 3, 4, 5]
    nums.reverse();
    console.log(nums); // [5, 4, 3, 1, 1]
      
  15. every() and some()

      let scores = [80, 90, 85];
    console.log(scores.every(s => s >= 60)); // true
    console.log(scores.some(s => s >= 90));  // true
      

Spread and Destructuring with Arrays

  let arr1 = [1, 2];
let arr2 = [...arr1, 3, 4]; // [1, 2, 3, 4]

let [first, second, ...rest] = [10, 20, 30, 40];
console.log(first, second, rest); // 10 20 [30, 40]
  

Multidimensional Arrays

  let matrix = [[1, 2], [3, 4]];
console.log(matrix[1][0]); // 3