How to delete variable in javascript

How to delete variable in javascript

Photo by Markus Winkler on Unsplash

In JavaScript, the delete operator is employed to delete a property of an object. After deleting the actual property, that property won’t be accessible and returns undefined.

The invocation of the delete operator returns true when it removes a property and false otherwise. it’s only effective on an object’s properties, it has no effect on variable or function names.

The delete operator shouldn’t be used on predefined JavaScript object properties like window, Math, and Date objects. It can crash your application.

Let’s scrutinize some facts about the delete operator.

Delete object properties

The only way to fully remove the properties of an object in JavaScript is by using delete operator.

How to delete variable in javascript

If the property which you’re trying to delete doesn’t exist, delete won’t have any effect and can return true.

How to delete variable in javascript

Can we delete variables in Javascript?

The delete operator removes a property from an object. It cannot delete a variable. Any property declared with var cannot be deleted from the global scope or from a function's scope.

How to delete variable in javascript

If you declare a variable without var, it can be deleted. Let’s look into the example below.

How to delete variable in javascript

The variable declared without the var keyword internally stores it as a property of the window object. So we can delete the properties of the window object.

Can we delete values from an array?

Since JavaScript arrays are objects, elements can be deleted by using delete.

How to delete variable in javascript

delete will delete the object property, but will not reindex the array or update its length. This makes it appear as if it is undefined.

Using delete may leave undefined holes in the array. Use pop(), shift(), or splice()instead.

Can we delete built-in objects?

How to delete variable in javascript

Deleting built-in objects like Math, Date, and window objects are unsafe, and they can crash your entire application.

Deleting non-configurable properties

Object properties, besides a value, has three special attributes:

  • writable – if true, the value can be changed, otherwise, it’s read-only.
  • enumerable – if true, it is listed in loops, otherwise not listed.
  • configurableif true, the property can be deleted or the attributes can be modified, otherwise, it cannot be changed.

How to delete variable in javascript

The values assigned by using Object.defineProperty and set to configurable: false in an object cannot be deleted.

How to delete variable in javascript

In strict mode, it will throw an error if you try to delete a non-configurable property.

How to delete variable in javascript


delete is the only true way to remove an object’s properties without any leftovers, but it works ~100 times slower if you are using delete in loops.

The alternative solution is setting the value to undefined like object[key] = undefined. It doesn’t fully delete the property, it just sets the value to undefined. the choice isn’t exactly a prominent solution, but if you utilize it with care then you’ll be able to improve the performance.

That’s all! I hope thistextmight help to perforate the delete operator

Thanks for reading :)

How do you delete a JavaScript variable?

To unset a variable in JavaScript, use the undefined. After that, use delete operator to completely remove it.

How do you delete a variable?

To delete a variable from all sessions, add a Remove-Variable command to your PowerShell profile. You can also refer to Remove-Variable by its built-in alias, rv . For more information, see about_Aliases.

How do you clear a variable value in Java?

Set clear() method in Java with Examples Set. clear() method is used to remove all the elements from a Set. Using the clear() method only clears all the element from the set and not deletes the set. In other words, we can say that the clear() method is used to only empty an existing Set.

What is delete in JavaScript?

The delete operator removes a given property from an object. On successful deletion, it will return true , else false will be returned.