Hacker News new | past | comments | ask | show | jobs | submit login

Stirred lots of controversy for having lopsided equality tables (ambiguity intended)



true == 1 === true

What's ambiguous about that? :-P


Of course this would not work.

typeof(true) is of type 'Boolean' and typeof(1) is of type 'number'.

Do they compare to the same type? Nope, therefore is false in both cases.

Try the following:

    let foo = true;
    true === foo === true; // returns true.


er - no. Assuming you are not trolling (benefit of the doubt and all that):

You might mean it shouldn't work? but alas it does (because of 'lopsided equality tables'). Try the following: open the console of your browser, type the following:

true == 1 === true

and press enter - tada! You can also check out this SO answer if you are sufficiently flabbergasted: https://stackoverflow.com/a/12236341


I'm not sure how what you linked is related, but the only "surprising" thing here is probably `true == 1`. This evaluates to true, and because comparisons are left-associative, we have `(true == 1) === true` which reduces to `true === true`, which, unsurprisingly, is true.


Agree. It was my point. `true 0== 1` resolves to true, but shouldn't so `true == 1 === true` should be false but is true, which could at the very least be called ambiguous to people not familiar with the language. It may reflect somewhat badly on Brendan since they felt compelled to introduce === to rectify this. Personally I think it made the language richer, with more options to chose from depending on circumstance, and it was more like an adaption to the new uses for js.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: