In JavaScript the language constructs 'Infinity', 'null', 'NaN' and 'undefined' have inconsistent casing.
Is it historical, or is there an intent behind it?
Both undefined and null are falsy by default. So == returns true. But when we use the strict equality operator (===) which checks both type and value, since undefined and null are of different types (from the typeof Operator section), the strict equality operator returns false.
If the parameter can't be converted to a number, Number(x) will return NaN 2. Therefore, if the conversion of parameter x to a number results in NaN , it returns true; otherwise, it returns false.
Javascript null represents the intentional absence of any object value. The undefined property indicates that the variable has not been assigned a value or not declared at all. The NaN property represents a “Not-a-Number” value. The NaN property indicates that a value is not a legitimate number.
The value null represents the intentional absence of any object value. It's never assigned by the runtime. Meanwhile any variable that has not been assigned a value is of type undefined . Methods, statements and functions can also return undefined .
Nobody knows. :-(
(Original answer follows)
Pure speculation, but…
null
undefined
aretrue
, false
);undefined
is a global property representing even more metawackery;Infinity
and NaN
are global properties reflecting IEEE floating-point sentinel values, and come straight (ish) from that third-party spec.So I can see why distinct case conventions may have come into play here: apples and oranges.
To my mind, the real question is why those last two are not Math.INFINITY
and Math.NAN
.
I think the reasons are:
In ECMAScript, types begin with uppercase:
Undefined, Null, Boolean, Number, String, Symbol, Object
Undefined and Null are two types which only have a single value, which has the same name as the type. But it would be too confusing if the case was also the same, so they used lowercase:
undefined
, null
NaN
has this casing because it's a IEEE 754-2008 “Not-a-Number” value
Not-a-Number ⟶ NaN
Infinity
can begin with uppercase because there is no type called Infinity. I guess it could also begin with lowercase, but maybe they wanted something analogous to NaN
(?)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With