srikanth_k
srikanth_k

Reputation: 2917

What is the value of x?

I have a piece of code that I wrote in tag.

<script>
 var y = 1,
 x = y = typeof x; 
alert(x);
</script>

This will alert x as "undefined".

Please explain me how this is evaluated by Javascript compiler.

Thanks in advance.

Upvotes: 0

Views: 47

Answers (2)

GMchris
GMchris

Reputation: 5648

Since x is not yet defined it will return 'undefined', then assign it to y and then assign the value of y (which is now undefined) to x.

Upvotes: 0

Paul
Paul

Reputation: 36319

Right to left. Type of x before it is defined is, not surprisingly, undefined.

If you alert y in the same place you alert x, it'll be undefined as well, since you've set it to type of x

Upvotes: 3

Related Questions