tc39 / proposal-optional-chaining

https://tc39.github.io/proposal-optional-chaining/
4.94k stars 75 forks source link

It may be good to note once, that a == null is actually a === null || a … #136

Closed KennethKinLum closed 4 years ago

KennethKinLum commented 4 years ago

It may be good to note once, that a == null is actually the same as a === null || a === undefined

I just found that out reading this code and dig into it further, but I posted it on Stack Overflow to see if other users know and it seemed like 50 out of 50 users didn't know that.

So if users read a == null, they might actually feel it is just comparing to null. A comment appearing once can help the situation.

It is interesting that greenhorns use ==, but as users get better, they learn to use === in JavaScript, and then it is more pro, if somebody start to use a == null to compare it to null or undefined.

claudepache commented 4 years ago

I just found that out reading this code and dig into it further, but I posted it on Stack Overflow to see if other users know and it seemed like 50 out of 50 users didn't know that.

Out of interest, could you provide a link to your post?

jridgewell commented 4 years ago

Appears to be https://stackoverflow.com/questions/60705515/in-javascript-is-using-a-null-in-fact-the-same-as-a-null-a-und.

Note that == null is is 99% equivalent, but there is a difference. document.all == null is true, but document.all === null and document.all === undefined are both false! document.all is loosely equal (==) to null, but not strict equal (===).

rkirsling commented 4 years ago

This is explained immediately above the codeblock:

The desugaring is not exact in the sense that ... that document.all should behave as an object.

As I expressed in #104, developers that aren't aware of document.all needn't be troubled by the subtle difference between == and === here.

KennethKinLum commented 4 years ago

This is explained immediately above the codeblock:

The desugaring is not exact in the sense that ... that document.all should behave as an object.

As I expressed in #104, developers that aren't aware of document.all needn't be troubled by the subtle difference between == and === here.

even with the comment saying null / undefined, many users don't know that a == null is actually good for comparing equality to null or undefined. Some may think it is just written simply to compare to null, and some may even think why == is used when the recommended compare operator is usually ===.

ljharb commented 4 years ago

Do many users not know that?

jridgewell commented 4 years ago

I don't think many people have ever heard of document.all

ljharb commented 4 years ago

I mean, do many users not know that == null checks both null and undefined?

KennethKinLum commented 4 years ago

Do many users not know that?

I think many JavaScript programmers were advised to use === in general and that == has many exceptional cases and try avoid using it, so they don't know much about using == and its special cases.

claudepache commented 4 years ago

The note that is proposed to be added is slightly incorrect (because of document.all), and it makes the note about document.all just above somewhat confusing. I have pondered about a possible formulation that would not be too cumbersome, and that would not confuse the precision about document.all, and I haven’t found something satisfying.

The fact that the operator tests for undefined and null is repeated several times:

If the operand at the left-hand side of the ?. operator evaluates to undefined or null...

and:

a?.b // undefined ifais null/undefined,a.botherwise.

When they stumble on something they don’t understand, a programmer is supposed to be able to find the answer by themself, either from the context (and in that particular case they have clear indices), or by consulting appropriate documentation about the language, just as you did.

So, I take the decision to close this issue as wontfix.