I used delete keyword to delete a variable but it doesn't seem to work....
var txt = "Some text";
alert(txt); //Output - Some text
delete txt;
alert(txt); //SAME OUTPUT - Some text
delete is used to delete properties, not variables. That is, it is used to remove a property from an object.
According to MDN's explanation of delete, "You can use the delete operator to delete variables declared implicitly but not those declared with the var or the function statement."
So the behaviour you've described is correct.
txt = undefeind.undefined. It does, however, allow what the variable previously referenced to be garbage collected unless there are other references to it (in other variables). There is a difference between a variable or property that doesn't exist and a variable or property that exists but has the "value" undefined.undefined will let the browser free up the memory because the thing the variable referenced can disappear - but it's like emptying a box: the box itself doesn't vanish into thin air.delete window.txt works if you created it with window.txt = ... in the first place, but not if created with var. And of course window only applies to global variables. It's good practice for unrelated reasons to avoid (too many) globals, so if you use a namespacing scheme your "variables" would actually be properties of your namespace object so you cold delete them.