View Full Version : Resolved How does JavaScript calculate values and in wich order?

11-09-2011, 03:16 PM

i=y=1;i<y; result:false (this makes sense to me)

Im wondering about how JavaScript does the following calculations to get those results? Please explain it if you do. Id appreciate your time and help. Thank you!

i=y=1;y<<i; result:2

i=y=1;y>>i; result:0

Philip M
11-09-2011, 03:32 PM
Have a look at


Left shift a << b
Shifts a in binary representation b (< 32) bits to the left, shifting in zeros from the right.

Sign-propagating right shift a >> b
Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off.

What results were you expecting?

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

11-09-2011, 03:44 PM
The << and >> are logical shift operators (left and respectively right); they are not arithmetical operators. That means your problem has nothing to do with the order of operations :)

See also:

Philip M
11-09-2011, 03:57 PM
In fact I can think of few situations where bitwise operators are of any real use in Javascript.

11-09-2011, 04:00 PM
I did not know what to expect. I just tried it and when i tried to figure it out my line of thought was something like this.

i=y=1;y<<i; The values of "i" and "y" is set to "1". "i" is greater than greater "y". The result should still be false if that was how JavaScript interpret "<<" as.) I dont know any practical use for this (my knowledge of programming is very limited) I was just being curious.

Now i see that it did interpret "<<" as something else.

It started with that JavaScript sets a value like this a=1, and it checks if av value is equal like this a==b then I just figured that greater than would be like this a>>b. Now i know that it is like this a>b. Then i was curious about what a>>b did.

I have no concept of what you just wrote but it helped me understand my question. Thank you!