I assume you executed the code within the Command Line.
So when you removed the var the second time you executed the code, x got
defined in the global scope. So when you added var again for the third time
the alert() showed you this globally defined x.
See also a related Stack Overflow
thread<http://stackoverflow.com/questions/1470488/difference-between-using-var-and-not-using-var-in-javascript>about
this.
Sebastian
On Wednesday, April 24, 2013 4:16:14 PM UTC+2, Trivender Ghangas wrote:
>
> I executed it three times:
>
> First time I executed it as it is and I didn't get any alert because
> variable x is not accessible outside function .
> Second time I removed the operator var and executed it . I got alert
> as expected(value of x).
> Third time when I execute it with the operator var and I get alert
> with the value of x.This is something strange .Please tell me why it
> is happening.
>
>
>
>
> function number()
> {
> var x=2;
> }
>
> number();
>
> alert(x)
>
--
--
You received this message because you are subscribed to the Google
Groups "Firebug" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
https://groups.google.com/forum/#!forum/firebug
---
You received this message because you are subscribed to the Google Groups
"Firebug" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.