How to delete a variable?

Delete a variable in JavaScript:

Summary:

The reason you are having trouble deleting your variable in JavaScript is because JavaScript won’t let you. You can’t delete anything created by the var command unless we pull a rabbit out our bag of tricks.

The delete command is only for object’s properties which were not created with var.

JavaScript will let you delete a variable created with var under the following conditions:

  1. You are using a javascript interpreter or commandline.

  2. You are using eval and you create and delete your var inside there.

Demo on terminal, Use the delete boo or delete(boo) command. A demo with js command line terminal let you 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 MUST set your variable to undefined in JavaScript, you have one option:

Demo in javascript page: put 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 sayings its 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>

Open myjs.html with a browser, it prints this:

aliens: undefined
typeof aliens: undefined
found some aliens: string
not sayings its aliens but... scramble the nimitz
aliens set to undefined
typeof aliens: undefined
you sure they are gone? undefined

Warning When you set your variable to undefined you are assigning a variable to another variable. If someone poisons the well by running undefined = 'gotcha!', then whenever you set your variable to undefined, it becomes: “gotcha!”.

How should we check if a variable has no value?

Use null instead of undefined 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);

Which prints:

skittles: undefined
typeof skittles: undefined
skittles: 5
typeof skittles:number
skittles: null
typeof skittles: object 

If you are not using strict, you can delete variables created like this:

<script type="text/JavaScript">
   //use strict
   a = 5;
   document.writeln(typeof a);        //prints number
   delete a;
   document.writeln(typeof a);        //prints undefined
</script>

but if you uncomment use strict, the javascript will not run.

Leave a Comment