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

Paraphrasing: "Well done, Javascript… for finding yet another way to confuse us all."

I don't know many programming languages, but of the few I know any of them has a === because == is so confusing that you don't know exactly if (or when) it would bite you. You can't attack any language for their comparison operators from the Javascript camp.

It that blog post removed the sentence "And this is how it would definitely happen if the code was written in javascript" they would be fine. But you actually don't know what would definitely happen in Javascript with the line in question "cell[0][0] == cell[1][1] == cell[2][2]", unless you know 100% what is in these cells. For example:

cell[0][0] is "0", cell[1][1] is 0, cell[2][2] is true

and "cell[0][0] == cell[1][1] == cell[2][2]" evaluates to True.




> cell[0][0] is "0", cell[1][1] is 0, cell[2][2] is true

If those values came from <input> elements, the second and third examples aren't possible without having manually converted it beforehand. Input values are always strings, even a checkbox. That's where javascript's conversion rules and lax equality came from, an attempt to have sane defaults for simple uses: If a string is being compared to a number, the string is likely from an input and the number was typed into the code, so convert the string to a number before comparison.




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

Search: