Skip to main content

The delete operator

The delete operator in JavaScript is used to remove properties from objects (or elements from arrays). But it has several subtle nuances worth knowing. Let's go through it in detail.


Syntax

javascript
delete object.property

or

javascript
delete object['property']

Example with an object

javascript
const user = { name: 'Tim', age: 25 }; delete user.age; console.log(user); // { name: 'Tim' } console.log('age' in user); // false

The delete operator removes the property itself from the object, not just its value.


Return value

delete returns a logical value:

CaseReturns
Deletion succeededtrue
Deletion is impossible (e.g., the property is "non-configurable")false

Example:

javascript
const obj = {}; Object.defineProperty(obj, 'x', { value: 10, configurable: false }); console.log(delete obj.x); // false console.log(obj.x); // 10

Example with an array

javascript
const arr = [10, 20, 30]; delete arr[1]; console.log(arr); // [10, <1 empty item>, 30] console.log(arr.length); // 3

Important: delete does not shift array elements, it simply removes the value, leaving a "hole" (empty).

If you need to remove an element while changing the array's length, use methods like:

javascript
arr.splice(1, 1); // removes the element and shifts the rest

Example with an object and undefined

The difference between delete and assigning undefined:

javascript
const user = { name: 'Tim', age: 25 }; user.age = undefined; console.log(user); // { name: 'Tim', age: undefined } console.log('age' in user); // true (the key remains) delete user.age; console.log(user); // { name: 'Tim' } console.log('age' in user); // false (the key is removed)

delete removes the key entirely undefined - just clears the value


Example with a prototype

delete does not affect properties inherited through the prototype:

javascript
const person = { name: 'Alex' }; const user = Object.create(person); console.log('name' in user); // true delete user.name; // won't delete from the prototype console.log('name' in user); // true

It only removes the object's own properties.


Limitations

  1. delete does not work on variables declared with var, let, const:
javascript
let x = 10; console.log(delete x); // false console.log(x); // 10

You can only delete a property of an object, not a variable.

  1. In strict mode ("use strict"), an attempt to delete a non-deletable property will cause an error:
javascript
"use strict"; delete Object.prototype; // TypeError

Summary

What it doesRemoves a property from an object
Works withObjects, arrays
Returnstrue or false
Does not work forVariables (var, let, const)
Does not shift array elementsLeaves a "hole"
Safe forAn object's own properties

In short

javascript
const obj = { a: 1, b: 2 }; delete obj.a; console.log(obj); // { b: 2 } console.log('a' in obj); // false

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.