## Question:

Explain, please, why out of the three alerts below, only the last one is executed? It seems like in all three cases there is a soft comparison for equality to `true`

```
if (true == "0") alert('Тру равно нулю!');
if ("0" == true) alert('Ноль равен тру');
if ("0") alert('Как бы тоже тру, или как?');
```

https://jsfiddle.net/z1xbshmk/

## Answer:

It is worth referring to the specification

*Evaluating EqualityExpression == RelationalExpression* returns the left and right values and applies the *Abstract Equality Comparison* to them.

The comparison x == y, where x and y are values, returns

trueorfalse. Such comparisons are made as follows:

IfType (x) isthe same asType (y), then Strict Equality Comparison x === y is returned.

Ifxisnullandyisundefined, returntrue.

Ifxisundefinedandyisnull, returntrue.

IfType (x)isNumberandType (y)isString, return the result ofx == ToNumber (y).

IfType (x)isStringandType (y)isNumber, return the result ofToNumber (x) == y.

IfType (x)isBoolean, return the result ofToNumber (x) == y.

IfType (y)isBoolean, return the result ofx == ToNumber (y).

IfType (x) isone of the following:String,Number, orSymbolandType (y)isObject, return the value of the expressionx == ToPrimitive (y).

IfType (x)isObjectandType (y) isone of the following:String,Number, orSymbol, return the value of the expressionToPrimitive (x) == y.

Returnfalse.

Let's take a look at the first example:

```
true == "0"
```

This expression matches the branch

If

Type (x)isBoolean, return the result ofToNumber (x) == y.

When reduced to a number, we get the expression: `1 == "0"`

we get into the branch

If

Type (x)isNumberandType (y)isString, return the result ofx == ToNumber (y).

When converted to a number, we get the expression: `1 == 0`

and as a result: *false* .

The second case is similar.

Now let's look at the third case.

Let's go back to the spec :

The `ToBoolean`

function is applied to the expression inside the brackets, which, in the case of a string, returns *false* if the string is empty (string length 0), and *true* otherwise.

Since "0" is not empty (line length 1), the condition is considered to be fulfilled.