1

A simple js snippet. Why does it always alert

'result is empty'

As per my understanding, if block should get executed since result!="" is true and alert

'result is not empty'.

<script>
var result = false;
if(result != "")
alert('result is not empty')
else
alert('result is empty')
</script>
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
  • Because "" is a falsy value. – jalooc Sep 29 '15 at 10:02
  • @jalooc No, that's not the reason – Bergi Aug 06 '19 at 20:18
  • @Bergi of course it is: `!=` casts values so that `false != ''` resolves to `false`. – jalooc Aug 08 '19 at 08:10
  • @jalooc But it casts `""` to a number (`Number("")`), not to a boolean (`Boolean("")`), so its falsiness doesn't matter. – Bergi Aug 08 '19 at 08:57
  • @Bergi the "0" number is still falsy, isn't it? Just execute `false != ''` in the devtools console. You'll get `false`, not `true` as you falsely state in your question. – jalooc Aug 09 '19 at 09:07
  • @jalooc The number `0` is not coerced a boolean anywhere, so its falsiness doesn't matter either? You can also try `"0" == false`, where `"0"` is definitely not a falsy value. – Bergi Aug 09 '19 at 10:26
  • @Bergi As I said, I meant the zero number, not the string. Zero as a string is truthy, whilst zero as a number is falsy. But that doesn't matter anyway, I was just referring to what you wrote about casting to a number. What matters though, is that `false != ''` evaluates to `false`, which is where you are wrong and why it alerts what it alerts. – jalooc Aug 10 '19 at 15:48
  • @jalooc I never claimed that `false != ''` wouldn't evaluate to `false`, all I am saying is that it has nothing to do with `''` being a falsy (i.e. `Boolean(…) === false`) value. – Bergi Aug 10 '19 at 19:10
  • "since `result!=""` is `true`" - isn't that what you wrote in the question? – jalooc Aug 11 '19 at 10:12

3 Answers3

3

It's because != does implicit type conversion. If you used the strict version, !==, it would do what you expect. But the loose version, !=, will convert both of those operands to numbers, and both "" and false convert to 0, so "" != false is false, because it ends up (through a series of convolutions) being 0 != 0.

This is laid out in detail in Abstract Equality Comparison algorithm in the specification:

  1. ReturnIfAbrupt(x).
  2. ReturnIfAbrupt(y).
  3. If Type(x) is the same as Type(y), then Return the result of performing Strict Equality Comparison x === y.
  4. If x is null and y is undefined, return true.
  5. If x is undefined and y is null, return true.
  6. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y).
  7. If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y.
  8. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y.
  9. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y).
  10. If Type(x) is either String, Number, or Symbol and Type(y) is Object, then return the result of the comparison x == ToPrimitive(y).
  11. If Type(x) is Object and Type(y) is either String, Number, or Symbol, then return the result of the comparison ToPrimitive(x) == y.
  12. Return false.

As we can see from the above, if we start out with false and "", then:

  • We follow Step 8, convert false to 0, and start again with 0 != ""
  • We follow Step 6, convert "" to 0, and start again with 0 != 0
  • We follow Step 3 and get the result false (because we're doing !=, whereas the algorithm is defined in terms of ==).
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

In javascript two falsy things can be equal, as in this case, where result != "" yields false.

You need to use an strict comparison, i.e. !== to make the comparison. result !== "" yields true.

JotaBe
  • 38,030
  • 8
  • 98
  • 117
0

Javascript checks for truthy values.

false != "" 

is false

false !== ""

is true so you can try it.

p.pradnya
  • 1,039
  • 1
  • 9
  • 9