Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear,

I have declare global variable in java script, i try to clear with following code but it not clear.

As i defined global variable.

var usno;

then before set value i tried following code

delete window.usno;
usno = undefined;
usno=null;

but no success.

Please help on this

Thanks
Basit.
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jul-15 1:08am    
Define "clear".
—SA
basitsar 19-Jul-15 10:32am    
many many thanks, Ok then can you please tell me how to clear global variable in java script as my above question

Simple. You can't.

A variable defined with var exists for the entire time it's in scope. Since you created a "global" variable, it's in scope everywhere so it'll never go away.

The best you can do is just set it to null. It won't remove the variable, but the contents.

The delete statement is for removing properties, not an entire variable.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jul-15 23:41pm    
You are right, but it's good to add that there is no need for "deleting" the variable.
Besides, the variable can be made underfined again; this is easy to check up.
Please see Solution 2.

Besides, the inquirer mostly asked how to "clear" the variable, whatever it means.
—SA
As Solution 1 explains, you cannot really delete a variable, and not because it is "impossible", but because the whole concept makes no meaning in JavaScript. When you write var usno, the variable is created and equal to undefined. It becomes accessible in that sense that any expression or statement using it won't generate a lexical error "usno is not defined". Here "defined" is used in a different sense that "undefined", which is referred to as the value of the variable.

The global variable cannot be "deleted" just because if is always in scope. At the same time, it can be made undefined again.
JavaScript
var usno; // not it is underfined
usno = null; // not undefined
usno = 3; // not undefined
usno = undefined; // undefined again

We made it undefined, but it remains existing variable, because it exists as such in lexical sense.

This is a delicate moment of JavaScript. It's a common misconception to consider it as a "pure interpreter" which only works line by line. No, at first the whole script comes through lexical analyzer; and it is easy to check up by making a lexical error. In this case, even if first part of script is correct, it won't be executed at all. Even the lexical errors can be caught as exceptions, but only when you run the interpreter itself. In my article, I explain how: JavaScript Calculator[^].

How is it related to "deletion" of the variable? The declared variable exists in a lexical scope and will exist as long as the scope exists. From the point of view of practice of programming, it means nothing. How a defined variable can prevent anything? It's just the name, nothing else. It does not occupy any space which should be made free of it. One can use it at any moment for any different purpose.

I do understand that you did not really try to "delete" the variable, if we forget your weird delete window.usno attempt, so my considerations on "deletion" was more in response to Solution 1 than to your question where you asked "how to clear". Then see also my comment to the question.

—SA
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900