delete Operator

JavaScript delete Operator

The `delete` operator is used in JavaScript to delete properties from an object or remove elements from an array. It allows you to remove a specific property or element and modify the structure of an object or array dynamically.

Deleting Object Properties

To delete a property from an object, you can use the `delete` operator followed by the object's property name:

const person = { name: 'John', age: 30 };
 
delete person.age;
 
console.log(person); // { name: 'John' }

In the example above, the `delete` operator is used to remove the `age` property from the `person` object. After deleting the property, the output will only include the `name` property.

Deleting Array Elements

To remove an element from an array, you can use the `delete` operator as well. However, be aware that using `delete` will remove the element but will not change the length of the array:

const numbers = [1, 2, 3, 4, 5];
 
delete numbers[2];
 
console.log(numbers); // [1, 2, empty, 4, 5]
 
console.log(numbers.length); // 5

In the example above, the `delete` operator is used to remove the element at index 2 from the `numbers` array. The output shows that the element is replaced with `empty`, indicating that the element has been deleted. However, the length of the array remains the same.

To completely remove an element and change the length of the array, you can use the `splice` method:

const numbers = [1, 2, 3, 4, 5];
 
numbers.splice(2, 1);
 
console.log(numbers); // [1, 2, 4, 5]
 
console.log(numbers.length); // 4

In this example, the `splice` method is used to remove one element at index 2 from the `numbers` array. The output shows that the array is modified, and the length is reduced by one.

Deleting Variable

It's important to note that the `delete` operator cannot delete variables declared with the `var`, `let`, or `const` keywords.

let x = 10;
 
var y = 20;
 
 
 
delete x; // false
 
delete y; // false
 
 
 
console.log(x); // 10
 
console.log(y); // 20

In the example above, attempting to delete variables `x` and `y` using the `delete` operator will return `false`, and the variables will not be deleted.

Deleting Built-in Object Properties

The `delete` operator can also be used to delete properties of built-in JavaScript objects, such as the `global` object (`window` in browsers):

delete window.document; // true
 
console.log(window.document); // undefined

In this example, the `delete` operator is used to delete the `document` property of the `window` object. After deleting the property, the output shows that the property is no longer accessible.

Key Takeaways

  • The `delete` operator is used to delete properties from objects or remove elements from arrays.

  • When used with objects, the `delete` operator removes the specified property.

  • When used with arrays, the `delete` operator removes the specified element but does not change the array's length.

  • Variables declared with `var`, `let`, or `const` cannot be deleted using the `delete` operator.

  • The `delete` operator can also be used to delete properties of built-in JavaScript objects.

Comments

Popular posts from this blog

Limitations of JavaScript

'for in' Loop

What is JIT compiler? Is JavaScript compiled or interpreted or both?