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** in JavaScript is used to **remove properties from objects** (or elements from arrays), but it has several subtle nuances. **Key point:** `delete` removes the key entirely (unlike assigning `undefined`, which only clears the value), does not work on variables (`var`, `let`, `const`), and does not shift array elements, leaving a "hole" instead.Shown above the full answer for quick recall.Answer (EN)ImageThe `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: | Case | Returns | |---|---| | Deletion succeeded | `true` | | 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**. 2. 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 does | Removes a property from an object | |---|---| | Works with | Objects, arrays | | Returns | `true` or `false` | | Does not work for | Variables (`var`, `let`, `const`) | | Does not shift array elements | Leaves a "hole" | | Safe for | An 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 ```For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.