Skip to main content

Command Palette

Search for a command to run...

Mastering the JavaScript delete Operator

Updated
2 min read
Mastering the JavaScript delete Operator

Introduction

In the world of JavaScript, think of the delete operator as your tool for making changes to objects and arrays. It allows you to remove specific things like properties in objects and elements in arrays. Let's explore how it works and what it can do.

Deleting Object Properties

Deleting a property from an object is a breeze. Just use the delete operator followed by the object's property name. For example:

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

In this example, the age property vanishes from the person object, leaving only the name property.

Deleting Array Elements

Similar to object properties, you can use the delete operator to remove elements from an array. However, it's important to note that using delete won't change the array's length:

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

Here, the element at index 2 is removed, but the length of the array remains the same. To fully remove an element and adjust the length, 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

Deleting Variables

It's important to understand that the delete operator cannot be used to delete variables declared with var, let, or const:

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

As seen in the example, attempting to delete variables x and y returns false, and the variables remain unaffected.

Deleting Built-in Object Properties

The delete operator is versatile—it can also be used to delete properties of built-in JavaScript objects, such as the global object (window in browsers). For example:

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

Here, the document property of the window object is deleted, rendering it inaccessible.

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.

Javascript

Part 4 of 24

In this series I will be sharing my journey in learning Javascript and posting my own notes from the process. These notes can be helpful for beginners in learning this amazing language. Good luck.

Up next

Mastering the 'in' Operator in JavaScript

Introduction In the world of JavaScript, there's a hidden detective known as the in operator. Its role? To determine whether a specific property exists within an object or if an elusive element is concealed within an array. This operator returns a si...

More from this blog

Dipankar Paul's blog

51 posts