Skip to content Skip to sidebar Skip to footer

How To Delete A Variable?

Here is my javascript code, I want to delete the variable code so that it has value undefined. var code = $(this).data('code'); var userelm = $(this); Here I'm doing the check: if

Solution 1:

Delete does not delete a variable.

The delete operator removes a property from an object.

You can try:

code = null;

Solution 2:

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.


Solution 3:

Try this in global scope:

x = 2;
delete window.x;

Solution 4:

The following works in strict mode also:

"use strict";

var foo = 1;
console.log(foo);
// 1

foo = (function(){}());
console.log(foo);
// undefined

;)


Solution 5:

delete doesn't affect variable names

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

x = 42;         // creates the property x on the global object
var y = 43;     // declares a new variable, y
myobj = {
  h: 4,
  k: 5
};

delete x;       // returns true  (x is a property of the global object and can be deleted)
delete y;       // returns false (delete doesn't affect variable names)
delete Math.PI; // returns false (delete doesn't affect certain predefined properties)
delete myobj.h; // returns true  (user-defined properties can be deleted)

delete myobj;   // returns true  (myobj is a property of the global object, not a variable, so it can be deleted)

Post a Comment for "How To Delete A Variable?"