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($('.code-1').val()!='' && $('.code-2').val()!='' && $('.code-3').val()!=''){
if(code==$('.code-1').val()+$('.code-2').val()+$('.code-3').val()){
$('.overlay').remove();
$('.code-box').remove();
$('.close-lock').remove();
userelm.ajaxloader(); //own function
userelm.off();
delete code;
console.log(code);
delete userelm;
}
}
Why does this program not remove the code
variable so it has the value undefined?
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:
-
You are using a javascript interpreter or commandline.
-
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.
[email protected] ~ $ 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.
Delete does not delete a variable.
The delete operator removes a property from an object.
You can try:
code = null;
Try this in global scope:
x = 2;
delete window.x;
The following works in strict mode also:
"use strict";
var foo = 1;
console.log(foo);
// 1
foo = (function(){}());
console.log(foo);
// undefined
😉
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)
Variables created on the fly will be automatically deleted after there is no more reference to it.
(function(){
var a = 1;
})(); //a available until here
The others answer is quite simple and effective
code = null;
code = undefined;
But I want to share it’s reason also, Why delete code; won’t work
delete is meant to delete properties of objects only. It’s never meant (developed) to delete variables.
var obj = {
'first' : 10;
}
//Following will print
console.log(obj.first)
delete obj.first;
//Following will print undefined/null/might give error
console.log(obj.first)
Bounus (for developers)
var item = 10;
delete item;
//Following will print
console.log(item);
//The reason for upper is when we declare variable with var it is of
//primitive data type and delete won't work.
item = 10;
delete item;
//Following will give reference error
console.log(item);
//The reason for upper is when we declare variable without var it is a
//property of global 'window' object so delete work.
because you write var for define the variable.
if you use var can not delete it.
you must be write this
userelm = null;
or don’t write var.
See this question: How to unset or remove a Javascript variable?
you don’t have to remove var , if you declared it in a function – when function ends, the inner var stops existing. You just don’t have to declare variables globally.
I was not successful with myvariable = null
.
But I was successful with myvariable = function(){};