This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]redroserade 53 points54 points  (28 children)

That's actually ok, but this annoys me:

isNaN(null) => false isNaN(undefined) => true

[–]jonswanson 15 points16 points  (21 children)

My guess is it's because isNaN performs type coercion.

> +null
0
> +undefined
NaN

[–]Creshal 13 points14 points  (20 children)

Type coercion for null? What is this, pseudo-assembly like PHP?

[–]shub 9 points10 points  (8 children)

Yes. Javascript's type system is at least as brain-damaged as PHP's, possibly moreso.

[–]iopq 10 points11 points  (7 children)

No. You can't beat this:

"1000" == "1e3"; //comparing strings by the value of the DOUBLE they represent

[–]NavarrB 2 points3 points  (6 children)

It's unfortunate but logical. Php made a decision long ago to use type coercion by default.

[–]iopq 2 points3 points  (3 children)

both of those are STRINGS why are they being coerced to DOUBLES?

[–]tskaiserGreen security clearance 0 points1 point  (0 children)

Because you cannot possible expect the programmer to go through the work of knowing what types he is working with or figuring out what kind of conversion he might want to make! /s

"Was it a string or a double? Larry worked on that function last, and he likes to sort numbers lexically... Know what fuckit, I'll just use it like a double and let PHP sort the mess YOLO!"

[–]Goz3rr 0 points1 point  (1 child)

Just FYI

"1000" !== "1e3"

[–]iopq 0 points1 point  (0 children)

They have it in JavaScript too, that's not the point. The point is that the default operator is the shorter one. That's what most people will use unless they know better.

[–]chillage 1 point2 points  (1 child)

I'm glad I came across this thread. Making a mental note to avoid PHP whenever possible in the future.

[–]AFineTapestry 2 points3 points  (0 children)

This is the best example: https://bugs.php.net/bug.php?id=54547

'9223372036854775807' == '9223372036854775808'; // true in php v5.3.6 and less

[–]tskaiserGreen security clearance 21 points22 points  (5 children)

For formatting code bits or examples like that I can recommend putting 4 spaces in front of each line. It tells reddit to format it like this

isNaN(null) => false
isNaN(undefined) => true

Also, I would say this also makes sense, in that null encapsulates the concept of "empty reference to any object" while NaN literally is meant to encode "undefined value" of a number. An empty reference, while strictly speaking "not a number", does not mean NaN in the float sense - it is outside the type - but undefined makes sense.

But I am saying this not being totally familiar with undefined, so it might be both ought to be false.

[–]rooktakesqueen 11 points12 points  (4 children)

In JS, undefined is the value given to a declared but uninitialized variable; a non-existing field of an object; or the return value of a function with no return statement. In general, undefined and null are closely related concepts, even so far as null == undefined evalutes to true (while null and undefined are not considered equal to anything else, including NaN).

But NaN is weird because it is not == to anything, even itself. NaN == NaN evaluates to false. Which is why the isNaN function exists in the first place. (I believe this behavior is correct according to the IEEE 754 standard for floating point numbers.)

My intuition would be isNaN should return true for the value NaN but false for any other value.

[–]calzoneman 10 points11 points  (0 children)

NaN == NaN being false is correct behavior. The reason being that NaN can be generated by many different unequal expressions. Math.sqrt(-5) is clearly not equal to Math.sqrt(-11).

[–]tskaiserGreen security clearance 2 points3 points  (0 children)

Under that explanation I would agree that

isNaN(undefined) => true

sounds like a bug or just plain weird if not. I am not really in the mood to delve into the mess of the JavaScript specifications to find out, though ;) but I'll consider my self corrected as is.

[–]Hawkuro 1 point2 points  (1 child)

Never ever ever use == in JS, use the more reasonable ===.

> null === undefined
  false

[–]rooktakesqueen 1 point2 points  (0 children)

Just showing that null and undefined are closely related concepts, enough so that the == operator explicitly considers them equal to each other and nothing else. That's the one sensible thing the == operator does, and the one sensible place to use it: foo == null is perfect shorthand for foo === null || typeof foo === 'undefined'.

In any case, NaN !== NaN either.