Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do 'Infinity', 'null', 'NaN' and 'undefined' have inconsistent casing?

Tags:

javascript

In JavaScript the language constructs 'Infinity', 'null', 'NaN' and 'undefined' have inconsistent casing.

Is it historical, or is there an intent behind it?

like image 548
thephpdev Avatar asked Jul 12 '16 15:07

thephpdev


People also ask

Why null === undefined is false?

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.

Why is NaN null 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.

What is the difference between NaN null and undefined?

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.

Why does JavaScript have both null and undefined?

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 .


2 Answers

Nobody knows. :-(


(Original answer follows)

Pure speculation, but…

  • null and undefined are is a JavaScript keywords, reflecting various "not a value" metawackery. All keywords I'm aware of are lowercase (c.f. true, 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.

like image 137
Lightness Races in Orbit Avatar answered Nov 11 '22 20:11

Lightness Races in Orbit


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 (?)

like image 38
Oriol Avatar answered Nov 11 '22 18:11

Oriol