delete operator

The delete operator removes a property from an object. After removal, this property will have an undefined value.

Syntax

javascriptdelete object.property;

Example

Let's create an object and remove one of its properties from it:

javascriptlet obj = {
  name: 'john',
  age:  23,
};

delete user.name;
console.log(user.name);

The code execution result:

javascriptundefined

Example

Let's create an array and remove an element from it:

javascriptlet arr = ['a', 'b', 'c', 'd'];

delete arr[2];
console.log(arr);

The code execution result:

javascript['a', 'b', empty, 'd']

See also

  • the push and unshift methods
    that add elements to an array
  • the splice method
    that also cuts off array parts, while modifying the array itself
  • the in operator
    that checks if an object or array has a property