Ranter
Join devRant
Do all the things like
++ or  rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Comments

theuser484322dCan you explain? I went through each line and they all seem good to me according to the spec.

That's how Mafia works.
Nobody would actually use such comparisons in an actual program. If you need to check an arbitrary input for validity, you'd most likely check the type first
typeof myVariable === "number"
and then for a valid value
0 <= myVariable
Number.isNaN(...) is not a general function to abuse for abitrary inputs. NaN means it's not a valid number in the range of numbers. E.g. some bit pattern which is not a valid doubleprecision floatingpoint in IEEE 754. 
melezorus34202522dNaN is a special kind of a number it's like how null is an object.
isNaN(arg) will try to convert the arg to a Number via Number(arg), and then check if it's a NaN via it's special comparison.
?: Number.isNaN, on the other hand, only checks if the argument is NaN, without correcting the type. Which will make it return false unless the arg is NaN.
parseInt(arg) will try to return as many digits as it can until it reaches something not digit.
Try parseInt on "d123" and "123" and see the difference. It's like regexp /^\d+/ . [1] 
theuser484322d@Demolishun JS is compliant to IEE 754 which allow distinct NaNs. Some maffs guy can probably clarify further how they work.

melezorus34202522dSince NaN is an exceptional value, you can't just compare it with another thing.
That's why isNaN function(s) exist.
a NaN compared to a number (which includes the NaN value) will return false no matter what.
But to be honest, I have never noticed that there was parseInt and parseFloat functions on the global, I always went to the Number object's methods.[2][END]
[2] 
AlgoRythm4653722dNaN is more like an exception object than a value. No two NaNs are the same, and they cannot be compared. It's confusing at first, but it has a straightforward and easytogoogle answer

Demolishun688122d@melezorus34 So maybe the comparison needs to check the type of NaN against type of returned value?

melezorus34202522dExtra:
?:How does one check if an argument is NaN without isNaN functions?
Easy, first of all, convert the argument to a number via Number() and save it somewhere.
Then, check if the number is not equal to 0 (I use type strict eq as a habit)
If it's not, return ! convertedArgument, NaN is a falsy value and ! operator will make it true.
If it's equal, return false as usual.
A shorter function can be written like this: @highlight
argument=>Number(argument)!=0 && !Number(argument); 
melezorus34202522d@Demolishun nope, it's a bit more complex. I just explained here without realizing you sent something btw.
As I said before, typeof NaN will give "number" too, so normal comparisons wouldn't cut. 
theuser484322d@melezorus34 I believe you are using the same parse functions, I cannot find any in the spec for Number so it might be resolving them in the prototype chain.

melezorus34202522d¯\_(ツ)_/¯ at least node is saying both functions are identical (===) and hasOwnProperty is giving true on both sides (Number, globalThis)

@Demolishun NaN is not comparable to NaN and it wouldn't make much sense anyway.
Let's say you want to compare two function parameters, a and b, which you expect to be numbers:
function someNums(a, b) {
if ( a === b ) { return 0; }
return a + b;
}
Since you expect both to be numbers, you should actually make sure of it, which is good practice:
function someNums(a, b) {
if (
typeof a !== 'number'  Number.isNaN(a)
&& typeof b !== 'number'  Number.isNaN(b)
) {
throw new Error( 'computer says "no"' );
}
if ( a === b ) { return 0; }
return a + b;
}
You don't need to do it everywhere but it helps a lot to find bugs quicker and the almighty V8 will optimize it anyway. 
Demolishun688122dI just read that you can check for NaN by comparing a variable to itself and it always returns false:
(function (){var x=NaN; return x===x;})()
If x is set to anything else it will return true.
This is a really interesting topic! 
theuser484322d@melezorus34 Looks like you were right, but according to MDN, Number's parse functions are not IE compatible.

melezorus34202522d

Root6350122dThere are many paths to creating NaN, such as imaginary numbers like 3+2i: `3 + (4)**(1/2)`, undefined realmath values like `Infinity * 0`, or undefined realjavascript values like `Number(undefined)`
because of this, the hypothetical "value" of each NaN would differ (which we do not and cannot store), so all comparisons therefore must fail. All NaNs are created unequal.
It's easy to demonstrate: `NaN + 2` returns `NaN`  and these are obviously different values! 
Demolishun688122d@Root But Infinity === Infinity is true. There are definitely different kinds of Infinity (positive and negative are 2).

Root6350122d@Demolishun In js, `Infinity == Infinity` is false, and `Infinity == Infinity` is true. This follows real math pretty accurately.
Infinity in math is a concept, not a value, so it cannot have a magnitude. (2*infinity) is infinity. therefore all infinities (in the same direction!) are equivalent.
The real mindbending aspects come in when raising it to a power, performing operations on/between two infinities (or better still: infinite series!). As JS doesn't know how to approach these, it resorts to NaN instead of calling you out on your mathematical transgressions. Afterall, if you wanted to do true rigorous math, you'd use something like R or Haskell instead 😅 
hack480422dWhen comparing a value to NaN I expected it works as comparing undefined values. I see now why there is isNaN function. But something is not clear for me. Why isNaN('') returns false?

theuser484322d@hack I assume you meant "". If you provide a non Number argument as the parameter, it is coerced to a Number. So a more explicit alternative to isNaN("") is isNaN(Number("")).
And what does Number("") return? It returns 0. 
hack480422d@Demolishun well, "yolo" is not a NaN object too but isNaN("yolo") returns true.
@theuser I see the problem lies under Number(""). Thanks. 
theuser484322d@hack Still not sure what the problem is though. Even without coercing to 0, an empty string still isn't a NaN.
Related Rants
Its true unless its javascript, then its false. What the fuck is this? Someone got explanation?
rant
js
jabbascript