4

jsbin started warning me that x != '' is not good, and I should replace it with x !== ''

Why?

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 2
    Wow, just check out all the related questions. So many questions titled "What's wrong with this javascript code". – ripper234 Jan 13 '12 at 11:02
  • 8
    If you bothered to look up the difference between `!=` and `!==` then you'd have your answer in seconds. I expect more from a 12.7k user who's been here for over three years. – Lightness Races in Orbit Jan 13 '12 at 11:03
  • 2
    Closers - don't close a question based on the user. The question is valid. – Oded Jan 13 '12 at 11:09
  • 1
    possible duplicate of [Why should I use string.length == 0 over string == "" when checking for empty string in ECMAScript?](http://stackoverflow.com/questions/1829611/), [Implied string comparison, 0='', but 1='1'](http://stackoverflow.com/questions/462663/), [JavaScript === vs == : Does it matter which “equal” operator I use?](http://stackoverflow.com/questions/359494/), [Javascript Comparison Operators != vs !== \[closed\]](http://stackoverflow.com/questions/8616483/) – outis Jan 13 '12 at 11:10
  • I agree with @Oded - if you can't find an actual duplicate (surely there *must* be one?), the question is perfectly valid. – T.J. Crowder Jan 13 '12 at 11:10
  • @outis: I don't think so, that's about using `length`, not `!=` vs. `!==`. – T.J. Crowder Jan 13 '12 at 11:10
  • @T.J.Crowder: the first is about why, which is the same as for this question. In any case, the others are explicitly about strict and type-converting equality. – outis Jan 13 '12 at 11:16
  • 1
    @outis: Thank you for finding the true duplicate (http://stackoverflow.com/questions/359494/javascript-vs-does-it-matter-which-equal-operator-i-use). – T.J. Crowder Jan 13 '12 at 11:17
  • 1
    @Oded: Questions with no research effort will be closed, not to mention the half a million duplicates. That I didn't feel like spending my time finding a specific one to closevote on doesn't change that this is obvious a duplicate, and anyone who's been on Stack Overflow for a few months knows that! :) – Lightness Races in Orbit Jan 13 '12 at 11:36

1 Answers1

7
var x = false;
console.log(x !== ''); //true
console.log(x != ''); //false

In other words, false (and other falsy values, like 0) 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:

If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.

The section on ToNumber tells us:

The result is 1 if the argument is true. The result is +0 if the argument is false.

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:

If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).

Calling ToNumber on an empty string results in +0, just as it did for false:

A StringNumericLiteral that is empty or contains only white space is converted to +0.

Now we are comparing +0 != +0, so we enter the "x and y are of the same type" section, which tells us:

If x is the same Number value as y, return true.

So +0 is equal to +0 and since we are using != it returns false.

James Allardice
  • 164,175
  • 21
  • 332
  • 312
  • 1
    @ripper234: So you need to know what each does (I recommend reading up - [strict equality](http://es5.github.com/#x11.9.6) | [loose equality](http://es5.github.com/#x11.9.3)) and then make a decision which to use in any given case. Both have their uses, although one should usually tend toward the strict ones (`===` and `!==`) much of the time. – T.J. Crowder Jan 13 '12 at 11:09