0

What is the diference between assigning value to declared variable and assigning value to non-declared variable ? I got a case where it have a difference.

Go to chrome browser console and put this code:

var x=8;
delete x; //it will return false because it is not deleted.
x; //and it return 8;

And then:

y=9;
delete y;// it return true here .because it is deleted. but in previous case that variable not deleted why ?
y; //it will return error:Uncaught ReferenceError: y is not defined.

I don't know why the variable which is declared is not deleted and variable which is not declared is deleted ?

Beyond this, is there any more difference between these two, then please specify.

Thanks!

mins
  • 6,478
  • 12
  • 56
  • 75
Mukund Kumar
  • 21,413
  • 18
  • 59
  • 79
  • do you define var x within a scope(function)? – Blauharley Apr 24 '15 at 19:33
  • 2
    Possible duplicates: http://stackoverflow.com/questions/28313794/delete-operator-confusion/28313829#28313829 and http://stackoverflow.com/questions/23379315/javascript-delete-statement/23379606#23379606 and a helpful article [Understanding Delete](http://perfectionkills.com/understanding-delete/). In general, you don't `delete` variables in Javascript. You delete properties from an object. Global variables in Javascript (e.g. undeclared variables) are a weird beast in Javascript because they are actually properties of the `window` object, thus they can sometimes be deleted. – jfriend00 Apr 24 '15 at 19:33
  • No. plese just put above code to chrome browser console. and then will get clear idea. – Mukund Kumar Apr 24 '15 at 19:35

0 Answers0