Introduction: This article is going to discuss the delete operator available in JavaScript. Delete is comparatively a lesser-known operator in JavaScript. This operator is more specifically used to delete JavaScript object properties.

The JavaScript pop(), shift() or splice() methods are available to delete an element from an array. But because of the key-value pair in an object, the deleting is more complicated. Note that, the delete operator only works on objects and not on variables or functions.

Syntax:

delete object
// or
delete object.property
// or
delete object['property']

This operator returns true if it removes a property. While deleting an object property that doesn’t exist will return a true but it will not affect the object. Though while trying to delete a variable or a function will return a false.

Example: Assuming an object called person has three key-value pairs (i.e. firstName, lastName and phone). Now, using the _delete _operator to delete the phone property will return true.

<script> 
    let person = { 
        firstName: "John", 
        lastName: "Doe", 
        phone: 12345 
    } 

    console.log(delete person.phone); 
    console.log(person); 
</script> 

Output:

As the above picture shows, delete person.phone returns true and logging the person object shows that the phone property doesn’t exist anymore.

#javascript #web technologies #javascript-operators #programming

JavaScript delete Operator
1.35 GEEK