Important Array Methods In JavaScript

Important Array Methods In JavaScript

Array:

  • JavaScript arrays are resizable and can contain mix of different data types.

  • Array elements are accessed using their indices.

  • Index starts from 0.

  • Javascript array-copy operation create shallow copies which means it shares the reference point of the original array. So changes made either in the copy or original array, reflect changes in both.

🔹Multiple ways to declare an array

  1. Using Array literal syntax:

let numbers = [1, 2, 3, 4, 5];
  1. Using Array Constructor:

let numbers = new Array(1, 2, 3, 4, 5);
  1. Using Array.of() method

let numbers = Array.of(1, 2, 3, 4, 5);

🔹Essential Array Methods in JavaScript

  1. push(element) :

    A method used to add one or more elements to the end of an array. This method modifies the original array. We can also push an array to another array.

     const marvelHeros = ["IronMan", "SpidarMan", "Captain-America"];
     const dcHeros = ["superman", "flash"];
    
     marvelHeros.push("Deadpool");    // push 1 element
     marvelHeros.push("Black-Widow", "Thor");    // push 2 elements
     dcHeros.push(marvelHeros);    // push an entire array
    
     console.log(marvelHeros);
     console.log(dcHeros);
     // Output: ["IronMan", "SpidarMan", "Captain-America", "Deadpool","Black-Widow", "Thor"]
     // Output: ["superman", "flash", ["IronMan", "SpidarMan", "Captain-America", "superman", "flash"]]
    
  2. pop() :

    A method used to remove the last element from an array. It also returns the removed element. It modifies the original array.

     let numbers = [1, 2, 3, 4, 5];
    
     // Removing the last element
     let removedElement = numbers.pop();
    
     console.log(removedElement); // Output: 5
     console.log(numbers); // Output: [1, 2, 3, 4]
    
  3. unshift(element) :

    A method used to add one or more elements to the beginning of an array. It modifies the original array.

     let fruits = ['apple', 'banana', 'orange'];
    
     // Adding elements to the beginning of the array
     fruits.unshift('grapes', 'kiwi');
    
     console.log(fruits); 
     // Output: ['grapes', 'kiwi', 'apple', 'banana', 'orange']
    
  4. shift() :

    A method which removes an element from the beginning of an array and also returns the removed element. It modifies the original array.

     let fruits = ['apple', 'banana', 'orange'];
    
     // Removing the first element
     let removedElement = fruits.shift();
    
     console.log(removedElement); // Output: 'apple'
     console.log(fruits); // Output: ['banana', 'orange']
    
💡
shift() and unshift() are expensive operation because they require reindexing of all elements in the array due to their operations at the beginning of an array.
  1. includes(searchElement) :

    A method to check whether the specified element is present in an array or not. Returns true if present and false on absent.

     const friendCircle = ["Kiran", "Deepika", "Aaditya", "Jyoti"];
    
     console.log(friendCircle.includes("Deepika"));    // Output: true
     console.log(friendCircle.includes("Prajakta"));    // Output: false
    
  2. indexOf(element) :

    A method used to find the index of a specified element in an array. Returns -1 if element is not present in an array.

     let numbers = [10, 20, 30, 40, 50];
    
     console.log(numbers.indexOf(30)); // Output: 2
     console.log(numbers.indexOf(70)); // Output: -1
    
  3. join(separator) :

    A method used to convert an elements of an array into string representation with custom separators. If you omit the 'separator' parameter, it defaults to using commas between elements.

     let fruits = ['apple', 'banana', 'orange'];
    
     let result = fruits.join(', ');
     console.log(result); // Output: 'apple, banana, orange'
    
     let numbers = [1, 2, 3, 4, 5];
    
     let numbersString = numbers.join(' - ');
     console.log(numbersString); // Output: '1 - 2 - 3 - 4 - 5'
    
  4. slice(startIndex, endIndex) :

    A method extracts a portion of an array and returns a new array containing the extracted elements. This method exclude the element present at givenendIndexand also does not change the original array.

     let numbers = [1, 2, 3, 4, 5];
    
     let slicedNumbers = numbers.slice(1, 4);
    
     console.log(slicedNumbers); // Output: [2, 3, 4]
     console.log(numbers); // Output: [1, 2, 3, 4, 5]
    
  5. splice(startIndex, deletecount, addNewElement) :

    A method performs operation similar to slice() method but only difference is that it modifies the original array. We can also add new elements using this method.

     let numbers = [1, 2, 3, 4, 5];
    
     // Removing elements
     let removedElements = numbers.splice(1, 2);
     console.log(numbers); // Output: [1, 4, 5]
     console.log(removedElements); // Output: [2, 3]
    
     // Adding elements
     numbers.splice(2, 0, 6, 7);
     console.log(numbers); // Output: [1, 4, 6, 7, 5]
    
  6. flat(depth) :

    A method is used to flatten a nested array structure. It creates a new array that contains all the elements of original nested array.

    Using Infinity as the depth parameter is particularly useful when you have arrays with unknown or deeply nested structures, and you want to ensure complete flattening of all nested arrays.

    let nestedArray = [1, 2, [3, 4]];
    let flatArray = nestedArray.flat(1);
    
    console.log(flatArray); // Output: [1, 2, 3, 4]
    
  7. Array.isArray(value) :

    A method used to check whether given value is array or not. Return boolean value depending upon the result.

    console.log(Array.isArray("Prajakta"));     // Output: false
    console.log(Array.isArray([2, 8, 9]));     // Ouput: true
    
  8. Array.from(value) :

    It allows you to convert array-like objects (such as strings or NodeList) or iterable objects (such as Map or Set) into arrays.

    let str = 'hello';
    let strArray = Array.from(str);
    
    console.log(strArray); // Output: ['h', 'e', 'l', 'l', 'o']
    

🔹Multiple ways to merge arrays

  1. concat(array1, array2) :

    A method used to merge two or more arrays. It returns a new array containing concatenated elements. It does not modify the original array.

     let arr1 = [1, 2, 3];
     let arr2 = [4, 5];
     let arr3 = ['a', 'b'];
    
     let mergedArray = arr1.concat(arr2, arr3);
     console.log(mergedArray); // Output: [1, 2, 3, 4, 5, 'a', 'b']
    
  2. Spread operator method :

    It allows you to easily copy array elements, merge arrays, pass function arguments, and more.

     let arr1 = [1, 2];
     let arr2 = [3, 4];
     let mergedArray = [...arr1, ...arr2];
    
     console.log(mergedArray); // Output: [1, 2, 3, 4]
    

💡
Keep an eye out for my article as it evolves with the latest information.