JavaScript Array and it’s methods

JavaScript Array and it’s methods

The Array object is used to store multiple values in a single variable.

const cars = ["Hyundai", "Honda", "BMW", "Audi"];

Some Important Array Methods

  1. concat():- Joins arrays and returns an array with the joined arrays

     const arr1 = ["Cecilie", "Lone"];
     const arr2 = ["Emil", "Tobias", "Linus"];
     const children = arr1.concat(arr2);
     console.log(children);
    
     //output: Cecilie,Lone,Emil,Tobias,Linus
    
  2. constructor:- Returns the function that created the Array object's prototype

     const fruits = ["Banana", "Orange", "Apple", "Mango"];
     let text = fruits.constructor;
     console.log(text);
    
     //output: function Array() { [native code] }
    
  3. filter():- Creates a new array with every element in an array that pass a test

     const ages = [32, 33, 16, 40];
    
     let result = ages.filter(checkAdult);
    
     function checkAdult(age) {
       return age >= 18;
     }
     console.log(result);
    
     //output: 32,33,40
    
  4. find():- Returns the value of the first element in an array that pass a test

     const ages = [3, 10, 18, 20];
    
     let result = ages.find(checkAge);
    
     function checkAge(age) {
       return age > 18;
     }
     console.log(result);
    
     //output: 20
    
  5. forEach():- Calls a function for each array element

     let text = "";
     const fruits = ["apple", "orange", "cherry"];
     fruits.forEach(myFunction);
    
     function myFunction(item, index) {
       text += index + ": " + item + "<br>"; 
     }
     console.log(text);
    
     //output: 0: apple
               1: orange
               2: cherry
    
  6. findIndex():- Returns the index of the first element in an array that pass a test

     const ages = [3, 10, 18, 20];
    
     const result = ages.findIndex(checkAge);
    
     function checkAge(age) {
       return age > 18;
     }
     console.log(result);
    
     //output: 3
    
  7. indexOf():- Search the array for an element and returns its position.

     const fruits = ["Banana", "Orange", "Apple", "Mango"];
     let index = fruits.indexOf("Apple");
    
     console.log(index);
     //output: 2
    
  8. length:- Sets or returns the number of elements in an array

     const fruits = ["Banana", "Orange", "Apple", "Mango"];
     let length =  fruits.length;
     console.log(length);
    
     //output: 4
    
  9. map():- Creates a new array with the result of calling a function for each array element

     const numbers = [4, 9, 16, 25];
     console.log(numbers.map(Math.sqrt));
    
     //output: 2,3,4,5
    
  10. pop():- Removes the last element of an array, and returns that element

    const fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.pop();
    console.log(fruits);
    
    output: Banana,Orange,Apple
    
  11. push():- Adds new elements to the end of an array, and returns the new length

    const fruits = ["Banana", "Orange", "Apple", "Mango"];
    fruits.push("Kiwi");
    console.log(fruits);
    
    //output: Banana,Orange,Apple,Mango,Kiwi
    
  12. slice():- Selects a part of an array, and returns the new array

    const fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
    const citrus = fruits.slice(1, 3);
    console.log(citrus);
    
    //output: Orange,Lemon
    
  13. splice():- Adds/Removes elements from an array

    const fruits = ["Banana", "Orange", "Apple", "Mango"];
    
    // At position 2, add 2 elements: 
    fruits.splice(2, 0, "Lemon", "Kiwi");
    console.log(fruits);
    
    //output: Banana,Orange,Lemon,Kiwi,Apple,Mango
    

    Thanks for reading this blog! 😊