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.
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.
We cannot undeclare a variable. However, we can set its value to undefined for the purpose
Example 1: This example describes delete keywords when var is not used.
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.
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.
- PHP | unset() Function
- PHP | Unset Session Variable
- PHP | Unset() vs Unlink() Function
- PHP | Program to delete an element from array using unset() function
- ES6 | Variables
- CSS | Variables
- SASS | Variables
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.