Suggest an editImprove this articleRefine the answer for “The delete operator”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**The `delete` operator removes an own property from an object, rather than just clearing its value.** It returns `true` when the removal succeeded and `false` when the property is non-configurable. In an array `delete` does not shift elements and does not change `length`, it leaves a hole (an empty item), so real removal needs `splice` or `filter`. It does not remove properties inherited through the prototype chain, nor variables declared with `var`, `let` or `const`. ```javascript const user = { name: 'Alice', age: 25 }; delete user.age; console.log(user); // { name: 'Alice' } console.log('age' in user); // false ``` **Key point:** `delete` removes the key itself from the object; assigning `undefined` leaves the key in place.Shown above the full answer for quick recall.Answer (EN)Image**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: | Case | Returns | | --- | --- | | The removal succeeded | `true` | | 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: | Question | Answer | | --- | --- | | What it does | Removes a property from an object | | What it works with | Objects, arrays | | What it returns | `true` or `false` | | What it does not work for | Variables (`var`, `let`, `const`) | | Arrays | Does not shift elements, leaves a hole | | Safe for | An 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.