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.
var usno;
usno = null;
usno = 3;
usno = undefined;
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