You-Dont-Know-JS icon indicating copy to clipboard operation
You-Dont-Know-JS copied to clipboard

Types & Grammar: Crazy Edge Cases

Open astericky opened this issue 12 years ago • 8 comments

Crazy Edge Cases: The sentences and code right before the crazy edge cases I don’t quite understand why coercion does not work on null and undefined objects. That part was somewhat confusing for me. Are you stating that undefined or null objects cannot be equal to each other?

astericky avatar Nov 16 '14 20:11 astericky

It's not really about coercion "not working on null and undefined" - it's about boxing not working with them. They are both coerced in special cases - see the linked spec section below.

The code example was:

var a = null;
var b = Object( a );    // same as `new Object()`
a == b;                 // false

var c = undefined;
var d = Object( c );    // same as `new Object()`
c == d;                 // false

var e = NaN;
var f = Object( e );    // same as `new Number( e )`
e == f;                 // false

So all that's being said here is that using new Object(null) and new Object(undefined) is the same as just calling new Object(), these parameters effectively get discarded. So there are no "undefined or null objects" (as you put it).

Looking at the spec, we can see that no case matches comparing an object to undefined, so the algorithm falls through until 10. and always returns false.

cobalamin avatar Nov 16 '14 20:11 cobalamin

The last case is odd in a different way that NaN is indeed wrappable inside a Number object -- but comparing it will unwrap it and effectively lead to a comparison like NaN === NaN, which is false (see 1.c in the spec, 11.9.3), as NaN is not equal to anything, not even itself.

cobalamin avatar Nov 16 '14 20:11 cobalamin

Ahh yes I get it now!!! Thanks for this. Should this item be explained better or will people understand this as it is written? I understood the NaN part because of the spec. However, the other part wasn't clear to me because I wasn't thinking of it in the right way.

astericky avatar Nov 16 '14 20:11 astericky

+1000! :)

getify avatar Nov 16 '14 20:11 getify

:)

@astericky just edited his comment right when you closed this – the question might be valid, is this explained in the best possible way? The real explanation is all in the comment

// same as new Object()

cobalamin avatar Nov 16 '14 20:11 cobalamin

Does that update help clarify? :)

getify avatar Nov 16 '14 21:11 getify

It does clarify it. It's that comment detail that makes the difference. I read and re-read that part and I was like ok I will move on and ask about it later. The comment should've helped me get it but I still read right over it. Perhaps maybe calling attention to the comment in the code explanation is a better way to draw people's attention to it.

astericky avatar Nov 16 '14 21:11 astericky

Did you read the updated text @astericky? Are you saying it's good now? I'm not sure... To me this rectifies the issues you just brought up. Looks like a concise and simple to understand correction.

cobalamin avatar Nov 16 '14 21:11 cobalamin