jsbin started warning me that x != '' is not good, and I should replace it with x !== ''
Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In other words,
false(and other falsy values, like0) will coerce to an empty string. The!==and===operators (strict equality operators) ensure that the things being compared are of the same type.To expand upon why this is the case, you need to head to the spec (linked to by T.J. Crowder in the comments). The section on the “Abstract Equality Comparison Algorithm” tells us the following:
The section on
ToNumbertells us:In the example above, the argument is
false, so we are now comparing+0 != ''. When comparing a number to a string, the following rule is followed:Calling
ToNumberon an empty string results in+0, just as it did forfalse:Now we are comparing
+0 != +0, so we enter the “x and y are of the same type” section, which tells us:So
+0is equal to+0and since we are using!=it returnsfalse.