JavaScript delete Operator (original) (raw)

Last Updated : 06 Jan, 2025

The **delete operator in JavaScript removes properties from objects, including inherited ones, and creates holes in arrays without changing their length. If a deleted property holds an object with no other references, that object is automatically released by JavaScript’s garbage collector over time.

**Syntax

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

**Parameter: It does not take any parameters.

**Return type: The delete operator returns true for non-existent properties but fails (false) when attempting to delete variables or functions.

**Example 1: In this example, we deletes the salary property from the emp object. If successful, it returns true. After deletion, console.log(emp) confirms the updated object without salary.

javascript `

let emp = { firstName: "Raj", lastName: "Kumar", salary: 40000 }

console.log(delete emp.salary); console.log(emp);

`

Output

true { firstName: 'Raj', lastName: 'Kumar' }

**Example 2: In this example, the age property in obj is made non-configurable, preventing deletion. delete obj.age returns false, and the age property remains unchanged in the obj object.

javascript `

// Define an object with a non-configurable property let obj = { name: "John" };

Object.defineProperty(obj, 'age', { value: 30, // Making 'age' property non-configurable configurable: false });

console.log(obj); // { name: 'John', age: 30 }

// Attempt to delete the non-configurable property 'age' let result = delete obj.age; console.log(result);

console.log(obj);

`

Output

{ name: 'John' } false { name: 'John' }

**Example 3: In this example, delete arr[0] removes the element at index 0, leaving an empty slot. The array retains its original length, but index 0 is now undefined.

javascript `

let arr = [1, 2, 3]

console.log(delete arr[0]); //true console.log(arr); //[empty, 2, 3]

`

Output

true [ <1 empty item>, 2, 3 ]

**Example 4: In this example, globalVar is declared with var and localVar with let. They cannot be deleted using the delete operator because variables declared using var, let, or const are not properties of the global object (in a browser environment, this would be window), but rather local variables or properties of the local scope.

JavaScript `

var globalVar = 10; let localVar = 20;

console.log(delete globalVar); // false, deletion fails console.log(delete localVar); // false, deletion fails

console.log(globalVar); // 10, deletion failed console.log(localVar); // 20, deletion failed

`

**Conclusion

Some developers use setting an object property’s value to null or undefined as an alternative method. However, this doesn’t fully remove the property; it still exists with the value of null or undefined. It can cause issues with operations like the ‘for…in’ loop. Additionally, using the delete operator in loops can significantly slow down the program. Therefore, it’s best to use delete only when it’s really necessary to remove an object property.