Skip to main content

The delete operator

The delete operator in JavaScript removes properties from objects (or elements from arrays). It removes the property itself, not only its value, and it has several subtleties: it does not shift array elements, it does not touch the prototype chain, and it does not work on variables.

Theory

TL;DR

  • Syntax: delete object.property or delete object['property'].
  • It removes the key itself: after delete, 'key' in obj is false.
  • It returns true on success and false when the property is non-configurable.
  • In an array it leaves a hole: length does not change and elements are not shifted.
  • It does not remove properties inherited through the prototype chain, only own ones.
  • It does not work on var, let or const variables; in strict mode a failed removal throws a TypeError.

Quick example

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

Syntax and an object example

javascript
delete object.property

or

javascript
delete object['property']

Example:

javascript
const user = { name: 'Alice', age: 25 }; delete user.age; console.log(user); // { name: 'Alice' } 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 boolean:

CaseReturns
The removal succeededtrue
Removal is impossible (for example, 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

Note that delete also returns true when the property did not exist at all, because after the operation the key is definitely absent.

Behaviour with arrays

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 and leaves a hole (empty).

If you need to remove an element and change the array length, use methods instead:

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

A sparse array left by delete behaves inconsistently: forEach, map and filter skip the holes, while for and for...of see undefined.

delete versus assigning 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 is still there) delete user.age; console.log(user); // { name: 'Tim' } console.log('age' in user); // false (the key is removed)

delete removes the key completely, while assigning undefined only clears the value. The difference shows up in Object.keys(), JSON.stringify() and in a for...in loop.

Prototypes, limitations and summary

delete does not touch properties inherited through the prototype chain:

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

It removes only the object's own properties.

Limitations:

  1. delete does not work on variables declared with var, let or const:

    javascript
    let x = 10; console.log(delete x); // false console.log(x); // 10

    Only an object property can be deleted, never a variable.

  2. In strict mode ("use strict") an attempt to delete a property that cannot be deleted throws:

    javascript
    "use strict"; delete Object.prototype; // TypeError

Summary table:

QuestionAnswer
What it doesRemoves a property from an object
What it works withObjects, arrays
What it returnstrue or false
What it does not work forVariables (var, let, const)
ArraysDoes not shift elements, leaves a hole
Safe forAn object's own properties

Common mistakes

  • Using delete to remove an array element. The length stays the same and a hole is left behind; use splice or filter.
  • Confusing delete obj.key with obj.key = undefined: in the second case the key remains and still shows up in Object.keys().
  • Expecting delete to remove an inherited property. You have to delete it from the object that actually owns it.
  • Reading the result of delete as "did the property exist": it returns true for a missing key as well.
  • Forgetting that in strict mode a failed removal throws a TypeError instead of quietly returning false.
  • Deleting keys in bulk in hot code: it changes the object's hidden class and slows it down; creating a new object with rest destructuring is often better.

Short Answer

Interview ready
Premium

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