How to unset JavaScript variables?
To know how to unset a variable we must know what are JS variables and how to declare a variable first. JavaScript variables are containers for storing data values. Variables can be declared using the ‘var’ keyword.
Example:
var x = 5; |
In the above code declaration, the value 5 has been assigned to the variable ‘x’. The unsetting of a variable means to destroy it once its purpose in the code has been fulfilled.
Here a question arises, do we really need to unset the variables in JavaScript once their job is done?
The answer is ‘No’. Also, it should be kept in mind that, variables set in the global scope cannot be deleted. It is advised to use var to declare variables in JS, however, the property declared using var keyword cannot be deleted as well. However, if the variable x was defined without using the ‘var’ keyword, then deleting it would have been possible using this method.
nxt = undefined; // window is used in JS to access // the global variables window.nxt = 'I am next'; delete window.nxt; |
As far as memory management goes, the JavaScript interpreter performs automatic garbage collection for memory management. The job of a garbage collector is to track memory allocation and find when a piece of the allocated memory is no longer needed, in such a case the memory will be freed automatically. The ‘Garbage Collector’ frees the programmer from the worries of the destruction or reallocation of the objects.
Also, as already mentioned above, the variable cannot be destroyed in JavaScript. So, what could be done now? Some developers may suggest using ‘delete’, but the delete operator removes a property from an object. It cannot remove a variable. So what’s the closest thing that can be done to unset a variable in JS?
We cannot undeclare a variable. However, we can set its value to undefined for the purpose
nxt = undefined; |
Example 1: This example describes delete keywords when var is not used.
<script> x = 20; delete x; console.log(x); </script> |
Output:

There will be no output in the console, as x gets deleted in this case. However, when var will be used to declare the value of x, the delete function will not work.
Example 2: The delete doesn’t work when var is used.
<script> var x = 20; delete x; console.log(x); </script> |
Output:

The console will display output as 20. ‘var’ has been used to initialize the value of x and thus ‘delete’ function will not work in this case.
Recommended Posts:
- Variables and Datatypes in JavaScript
- How to pass JavaScript variables to PHP ?
- Global and Local variables in JavaScript
- What are undeclared and undefined variables in JavaScript?
- How to pass variables and data from PHP to JavaScript ?
- How to unpack array elements into separate variables using JavaScript ?
- JavaScript Course | Variables in JavaScript
- PHP | unset() Function
- PHP | Unset() vs Unlink() Function
- PHP | Program to delete an element from array using unset() function
- CSS | Variables
- Python Variables
- Variables in TypeScript
- Perl | Variables
- How to pass PHP Variables by reference ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



