NaN is neither greater than nor less than any value

Have you ever been in a situation where you’re expecting something and you get something unexpected in return?
Well, I’m not talking about “the mysteries of life” but “the mysteries of Javascript”.

Consider we’ve something like this below:

let x = …


This content originally appeared on DEV Community and was authored by Kush

Have you ever been in a situation where you're expecting something and you get something unexpected in return?
Well, I'm not talking about "the mysteries of life" but "the mysteries of Javascript".

Consider we've something like this below:

let x = 9;
let y = "11";
let z = "14";

x < y; // true
y < z; // true
z < x; // false

x < y
So, what's exactly happening here is, if wither of the values or both are strings i.e, when x < y, then both the values are coerced to be numbers and a typical numerical comparison happens.

y < z
But if both the values in the < comparison are strings, the comparison is made lexicographically same as the order you find in any dictionary.

Let's look at another case when one of the values can't be made into a valid number.

let x = 11;
let y = 'z';
x < y; // false
x > y; // false
x == y; // false

Most of us should be confused, how all the three comparisons be false? but it's okay to be confused here. The thing to catch here is value y is being coerced to 'Not a Number' (NaN) in the equality comparisons and ** NaN is neither greater than nor less than any other value**.

Ref: YDKJS


This content originally appeared on DEV Community and was authored by Kush


Print Share Comment Cite Upload Translate Updates
APA

Kush | Sciencx (2024-06-24T19:13:15+00:00) NaN is neither greater than nor less than any value. Retrieved from https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/

MLA
" » NaN is neither greater than nor less than any value." Kush | Sciencx - Monday June 24, 2024, https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/
HARVARD
Kush | Sciencx Monday June 24, 2024 » NaN is neither greater than nor less than any value., viewed ,<https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/>
VANCOUVER
Kush | Sciencx - » NaN is neither greater than nor less than any value. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/
CHICAGO
" » NaN is neither greater than nor less than any value." Kush | Sciencx - Accessed . https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/
IEEE
" » NaN is neither greater than nor less than any value." Kush | Sciencx [Online]. Available: https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/. [Accessed: ]
rf:citation
» NaN is neither greater than nor less than any value | Kush | Sciencx | https://www.scien.cx/2024/06/24/nan-is-neither-greater-than-nor-less-than-any-value/ |

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.