JS WTF: 5 < 4 < 3

A quick fun "JS WTF?" post for you today. If you load up your JavaScript console & enter:
5 < 4 < 3

You’d be expecting to see false, right? However, you’ll actually see true. WTF?
This is actually down to the way JavaScript evaluat…


This content originally appeared on Jack Franklin and was authored by Jack Franklin

A quick fun "JS WTF?" post for you today. If you load up your JavaScript console & enter:

5 < 4 < 3

You'd be expecting to see false, right? However, you'll actually see true. WTF?

This is actually down to the way JavaScript evaluates this and operator precedence. What it sees is:

(5 < 4) < 3

Which in turn gives

false < 3

JavaScript then coerces false into an integer 0:

0 < 3

And zero is indeed less than 3, so we get true returned.

Not much learned from this one but it's quite a fun thing to show someone & then explain why. I'll be trying to do a lot of these small "fun" posts as there's a fair few areas of "WTF?" in JavaScript, as we all know.


This content originally appeared on Jack Franklin and was authored by Jack Franklin


Print Share Comment Cite Upload Translate Updates
APA

Jack Franklin | Sciencx (2012-04-10T00:00:00+00:00) JS WTF: 5 &lt; 4 &lt; 3. Retrieved from https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/

MLA
" » JS WTF: 5 &lt; 4 &lt; 3." Jack Franklin | Sciencx - Tuesday April 10, 2012, https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/
HARVARD
Jack Franklin | Sciencx Tuesday April 10, 2012 » JS WTF: 5 &lt; 4 &lt; 3., viewed ,<https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/>
VANCOUVER
Jack Franklin | Sciencx - » JS WTF: 5 &lt; 4 &lt; 3. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/
CHICAGO
" » JS WTF: 5 &lt; 4 &lt; 3." Jack Franklin | Sciencx - Accessed . https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/
IEEE
" » JS WTF: 5 &lt; 4 &lt; 3." Jack Franklin | Sciencx [Online]. Available: https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/. [Accessed: ]
rf:citation
» JS WTF: 5 &lt; 4 &lt; 3 | Jack Franklin | Sciencx | https://www.scien.cx/2012/04/10/js-wtf-5-lt-4-lt-3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.