Global object Array itself does not have any remove or delete method. But there are many other ways to remove array elements in JavaScript.

Below are the different ways to remove array elements in JavaScript.

  1. From the start of an array using shift() method.
  2. From the end of an array by setting array length set or pop() method.
  3. Removing a range of elements from anywhere using splice() method.
  4. Removing elements by value using splice() and filter() method.
  5. By using Lodash library. (This requires loadash library to be included in your script: https://lodash.com)
  6. Using delete operator.

Let’s see those different ways one by one:

  1. Removing elements from the start:
    • Using shift() method: [Removes only one element]
      It removes the first element of an array and updates array length and element indexes.
      It also returns the removed element on execution.
  2. Removing elements from the end:
    • Setting array length: [Removes single or multiple elements]
      Set array length property to the desired length (less than actual array length) and rest of the elements will be removed from the end of an array.
      This way, it will remove elements with an index greater than equal to the desired length.
    • Using pop() method: [Removes only one element]
      It removes the last element of an array and updates array length and element indexes.
      It also returns the removed element on execution.
  3. Removing a range of elements from anywhere:
    • Using splice() method: [Removes any number of elements from anywhere in an array]
      • It removes any number of elements from anywhere in an array and updates array length and element indexes.
      • It also returns all removed elements as a new array.
      • It requires arguments to be passed (zero-based index).
        Only the 1st argument is mandatory which specifies the position from where to add/remove elements.
        The 2nd argument specifies the number of elements to be removed.
        The 3rd  argument and rest of the arguments after that specifies new elements to be added to the array.


  4. Removing elements by value:
    • Using splice() method:
      In this approach, you can loop through the array to check if the element you want to remove matches with the element currently being looped and then remove it.
    • Using filter() method:
      This does not affect the original array and will return a completely new array with removed elements.
      so basically, it will filter elements which are matched from the original array.

  5. Using Lodash library: (This requires loadash library to be included in your script: https://lodash.com)
    Libraries are a sometimes better way to solve complex problems.
    Lodash provides such rich set of array manipulation.
    It works somewhat similar to the array filter() method but it also updates the original array by removing elements and returning those elements as a new array.
  6. Using delete operator:
    So far we have seen many ways which delete the elements, update array length and indexes of the elements.
    But delete operator just removes the reference to the element so that it becomes undefined.
    Memory is freed when there are no more references to a value so delete operator frees the memory instead of deleting the element.

 

So these are all different ways to remove array elements in JavaScript.
I hope it will help you write better array manipulation codes as and when required based on the problems to be solved.

Happy coding!!!