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?
Using rm() command: When you want to clear a single variable from the R environment you can use the “rm()” command followed by the variable you want to remove.
You cannot delete a variable in C, nor in C++. A variable disappears when it goes out of scope. Perhaps you mean that you have a pointer variable, and it is currently pointing to a memory address you got back from a call to something like malloc().
To delete a variable, it uses keyword “del”.
there is no way to "delete" it. It is either a global variable, with statically allocated storage, or it is a local variable with storage on the stack.
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.
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!".
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With