-8

I am curious how

'' == '0'  // false

left side is a blank string and right side has string with value 0 so its fine its is false.

But

0 == ''    // true

how blank string equals to zero, both are typed different as well as value.

similar

false == 'false'   // false

left is false , but we are not equating (===) type of value , its just has value false so it should be true ? but why it is false.

Could we explain the same ?

Mayank
  • 934
  • 1
  • 17
  • 37

2 Answers2

1

The left operand is of the type Number. The right operand is of the type String. In this case, the right operand is coerced to the type Number:

0 == Number('')
which results in

0 == 0
nitin
  • 156
  • 2
  • 13
0

The following values are always falsy:

  • false 0 (zero)
  • "" or '' (empty string)
  • null
  • undefined
  • NaN (a special Number value meaning Not-a-Number!)

I found a nice table explaining what works and what not with the == operator.

enter image description here

Here is the link to the tables also containing === and if().

Starfish
  • 3,344
  • 1
  • 19
  • 47
  • Just because something is falsy does not explain the rules used when the `==` operator is applied. – zerkms Aug 18 '15 at 09:39
  • "The empty string is a falsy value, the string containing 0 is not the integer 0 and not empty so the string is true" --- this is wrong. – zerkms Aug 18 '15 at 09:40
  • "0 (zero) is a falsy value. An empty string ('') is a falsy value too" --- this is wrong as well. – zerkms Aug 18 '15 at 09:40
  • 1
    Guys, please **STOP** trying to guess, check the specification **BEFORE** you give answer about language behaviour. Thank you for saving our time. – zerkms Aug 18 '15 at 09:40
  • An empty string and an integer 0 is a falsy value. You forgot we are comparing with 2 equal signs, not 3. The 3rd equals sign compares the type too. In this case it doesn't. – Starfish Aug 18 '15 at 09:41
  • Please check and make sure you understand the link http://es5.github.io/#x11.9.3 **before** you continue this useless discussion. What you use as an argument is just nonsense. – zerkms Aug 18 '15 at 09:42
  • And now it does not address the question in any way. The question was why `==` operator behaves like that. Whereas your answer mentions some term that is not directly relevant to that operator. – zerkms Aug 18 '15 at 09:47