Removing a Variable in JavaScript:
Summary:
If you're struggling with deleting a variable in JavaScript, it's because JavaScript doesn't make it easy. The var
command creates variables that cannot be deleted easily unless you resort to some advanced techniques.
The delete command is meant for properties of objects that weren't created using var.
JavaScript allows deletion of a variable created with var under specific conditions:
You're using a JavaScript interpreter or command line.
You're utilizing eval and creating/deleting the var within there.
To see a demonstration on the terminal, use the delete boo
or delete(boo)
commands. A demo using the js
command line terminal can successfully delete a variable.
el@defiant ~ $ js
js> typeof boo
"undefined"
js> boo
typein:2: ReferenceError: boo is not defined
js> boo=5
5
js> typeof boo
"number"
js> delete(boo)
true
js> typeof boo
"undefined"
js> boo
typein:7: ReferenceError: boo is not defined
If you absolutely need to set your variable to undefined in JavaScript, here's one method:
In a JavaScript page, include this in myjs.html
:
<html>
<body>
<script type="text/JavaScript">
document.write("aliens: " + aliens + "<br>");
document.write("typeof aliens: " + (typeof aliens) + "<br>");
var aliens = "scramble the nimitz";
document.write("found some aliens: " + (typeof aliens) + "<br>");
document.write("not saying it's aliens but... " + aliens + "<br>");
aliens = undefined;
document.write("aliens set to undefined<br>");
document.write("typeof aliens: " + (typeof aliens) + "<br>");
document.write("you sure they are gone? " + aliens);
</script>
</body>
</html>
Upon opening myjs.html in a browser, it will display:
aliens: undefined
typeof aliens: undefined
found some aliens: string
not saying it's aliens but... scramble the nimitz
aliens set to undefined
typeof aliens: undefined
you sure they are gone? undefined
Warning: Setting a variable to undefined
essentially assigns it to another variable. If someone tampers with undefined
by running undefined = 'gotcha!'
, then setting your variable to undefined will result in: "gotcha!".
How to Determine if a Variable has No Value:
Instead of using undefined, consider using null like this:
document.write("skittles: " + skittles + "<br>");
document.write("typeof skittles: " + (typeof skittles) + "<br>");
var skittles = 5;
document.write("skittles: " + skittles + "<br>");
document.write("typeof skittles:" + typeof skittles + "<br>");
skittles = null;
document.write("skittles: " + skittles + "<br>");
document.write("typeof skittles: " + typeof skittles);
This code snippet results in:
skittles: undefined
typeof skittles: undefined
skittles: 5
typeof skittles:number
skittles: null
typeof skittles: object
If you're not using strict mode, you can delete variables created in this manner:
<script type="text/JavaScript">
//use strict
a = 5;
document.writeln(typeof a); //prints number
delete a;
document.writeln(typeof a); //prints undefined
</script>
However, uncommenting use strict
will prevent this JavaScript code from executing.