3

If 0 (as a digit) equals to 0 as a string, and 0 equals to an empty list, why 0 as a string is not equal to an empty list (from the moment that 0 as a digit equals to 0 as a string)?

> 0 == '0'
true
> 0 == []
true
> '0' == []
false
LoukasPap
  • 1,244
  • 1
  • 8
  • 17

4 Answers4

3

To understand the results of your code, you need to understand how == equality operator works in the following cases:

  • When both operands are of same type, return the result of strict equality comparison ===
  • If one operand is a number and other is a string, convert the string operand to a number
  • If any or both operands are non-primitive, convert non-primitive operand to a primitive value

Keeping the above mentioned points in mind, lets see how each comparison is processed:

  1. 0 == '0'

    This evaluates to true because '0' is converted to a number and then strict equality comparison is performed since both operands have the same type after the conversion of '0' to a number.

  2. 0 == []

    returns true because when any operand of the == operator is non-primitive, it is converted in to a primitive value. Empty array when converted into a primitive value, results in an empty string. So after this conversion, you have 0 == ''. Now one operand is a number and other is a string. Empty string is then converted to a number which results in 0 leading to the comparison of 0 == 0 which evaluated to true.

  3. '0' == []

    this returns false because empty array is converted to a primitive value which is an empty string. When [] is converted to an empty string, types of both operands ('0' == '') are same, so strict equality comparison is done which returns false.

For details, see Ecmascript Spec: 7.2.15: Abstract Equality Comparison

Yousaf
  • 27,861
  • 6
  • 44
  • 69
0

In JavaScript, double equals does not mean strictly equal to, whereas tripple equals === does. In the former cases a conversion is made as a result of the types involved, whereas no such available conversion (which results in semi-equality) exists in the latter case.

Hi - I love SO
  • 615
  • 3
  • 14
0

JavaScript is designed that way. In 0 == '0' == converts '0' to 0 because the right operand is a number So what happens is it checks 0 == 0 which is true. Tips use === called strick equality comparison to check, which will treat 0 as a number and '0' as a string.

0 == [] In Javascript empty array is treated as a falsy, while 0 is always false and 1 truth.

So == compares false == false which is true. Once again use strict equity sign ===

'0' == []

Here since [] is not a number Javascript won't care to convert '0' to 0 which means the comparison is comparing string and array which will won't be true.

Fritzdultimate
  • 372
  • 2
  • 11
0

. You have 3 cases.

  1. when you write '0' == 0 , the Value of '0' in utf-8 is 0 hence
    false ==false is true

  2. String is basically a list in python when you write '0' you are basically making a list with an element ' '0' ie '0' ==['0'] and not [ ] hence true==false is false

  3. while when you write 0==[ ] every value with 0 is false and the empty list returns the Boolean value of number of elements in the list which is False for 0 hence false ==false is true