3

I am confused about the statement below about the equal comparison operator ==

If the two operands are not of the same type, JavaScript converts the operands then applies strict comparison. If either operand is a number or a boolean, the operands are converted to numbers if possible; else if either operand is a string, the other operand is converted to a string if possible. If both operands are objects, then JavaScript compares internal references which are equal when operands refer to the same object in memory.

However both alert(true == "true") and alert(false == "false") returns false. Why is this so?

If either operand is a number or a boolean, the operands are converted to numbers if possible

In what case does JavaScript converts true and "true" to a number? The previous example doesn't convert them to numbers.

I have more doubts when looking at these examples:

''        ==   '0'           // false
0         ==   ''            // true
0         ==   '0'           // true
false     ==   'false'       // false
false     ==   '0'           // true
  • '' is false and '0' is true, but why the output is false?
  • 0 would be interpreted as false, and '' is also false. Then it becomes true. I can understand this.
  • Why does a '0' interpreted as true when 0 is false?
  • Why is false interpreted as false and '0' interpreted as true?

Could someone explain in more detail how the == operator works?

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
user944513
  • 12,247
  • 49
  • 168
  • 318

3 Answers3

4
=== // strict check. Will compare value and type

== // will check only value not type

So,

0         ==    ''            // true

0         ===   ''            // false

The == operator will compare for equality after doing any necessary type conversions. The === operator will not do the conversion, so if two values are not the same type === will simply return false. It's this case where === will be faster, and may return a different result than ==. In all other cases performance will be the same.

Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?

Community
  • 1
  • 1
Deepak Ingole
  • 14,912
  • 10
  • 47
  • 79
2

Why true =="true" and false=="false" is false?

If either operand is a number or a boolean, the operands are converted to numbers if possible

the boolean true can be converted to one .

console.log(Number(true)); // 1

Lets convert the second argument

console.log(Number("true"));  // NaN . "True" cannot be converted to a number

so this evalutes to 1==NaN // false. NaN is crazy because its not even equal to itself NaN==NaN

Same is the case with false =="false"

All your other queries can be solved using the same deduction. try expliclity converting your arguments based on the rules and check its Value in console.

eg.) console.log(TYPE(arg)) // Type is either String or Number

jozzy
  • 2,863
  • 3
  • 15
  • 12
1

Take a look at the javascript documentation. You want to use === instead of ==

=== checks for value and type equality.

From the documentation:

Equality (==, !=)

If the types of the two expressions are different, attempt to convert them to string, number, or Boolean.

NaN is not equal to anything including itself.

Negative zero equals positive zero.

null equals both null and undefined.

Values are considered equal if they are identical strings, numerically equivalent numbers, the same object, identical Boolean values, or (if different types) they can be coerced into one of these situations.

Every other comparison is considered unequal.

Identity (===. !==)

These operators behave identically to the equality operators except no type conversion is done, and the types must be the same to be considered equal.

Cacho Santa
  • 6,846
  • 6
  • 41
  • 73