How to usedelete Operator in Javascript

This operator is more specifically used to delete JavaScript object properties.

Example: In this example, we will use JavaScript delete operator to delete items from an array.

Javascript




const arr = [2, 7, 9, 15, 19];
delete arr[3];
console.log(arr);


Output

[ 2, 7, 9, <1 empty item>, 19 ]

Different ways to delete an item from an array using JavaScript

In Javascript, we do not have any array.remove() method for deleting the element. we will have an array and we need to delete a given item from that array and return the resulting array in the console.

These are the following methods for solving this problem:

Table of Content

  • Using for loop and push() Method
  • Using Pop() Method
  • Using shift() Method
  • Using splice() Method
  • Using filter() Method
  • Using delete Operator
  • Using Lodash _.remove() Method

Note: There are some other methods that are created by JavaScript inbuilt methods.

Similar Reads

Method 1: Using for loop and push() Method

This method will not mutate the original array. First, you have to create an empty() array and then loop over the new array and push only those elements that you want....

Method 2: Using Pop() Method

...

Method 3: Using shift() Method

This method is used to delete the last element of the array and return the deleted item as an output. Removing the element decreases the length of the array....

Method 4: Using splice() Method

...

Method 5: Using filter() Method

This method is used to delete an element from the beginning of an array. This method is used to return the first element of an array. It also decreases the length of the original array....

Method 6: Using delete Operator

...

Method 7: Using Lodash _.remove() Method

This method is used for deleting the existing element or replacing the content of the array by removing/adding a new element....

Contact Us