Technical Article => Web => JavaScript

JavaScript is a weak typed language and it has a loose comparison feature where two objects/values of different type can be compared using == operator. This provides developers great flexibility and confusion at the same time.

Before understanding how == works in JavaScript, can you first answer the question in the post title? Can a == true && a == false be true in JavaScript? Normally, we would think that this expression will always return false since a can be either true or false but cannot be both. However, in JavaScript, the above expression can return true indeed.

If we have below code, we will get the above expression to return true:

const a = { num: 1, valueOf: function(){ return this.num--; } }; console.log(a == true && a== false);

To understand why above code returns true, the explanation of how == works in JavaScript is needed. According to ECMAScript Spec, below describes the steps for performing == comparison.

If Type(x) is the same as Type(y), then Return the result of performing Strict Equality Comparison x === y. If x is null and y is undefined, return true. If x is undefined and y is null, return true. If Type(x) is Number and Type(y) is String, return the result of the comparison x == ToNumber(y). If Type(x) is String and Type(y) is Number, return the result of the comparison ToNumber(x) == y. If Type(x) is Boolean, return the result of the comparison ToNumber(x) == y. If Type(y) is Boolean, return the result of the comparison x == ToNumber(y). If Type(x) is either String, Number, or Symbol and Type(y) is Object, return the result of the comparison x == ToPrimitive(y). If Type(x) is Object and Type(y) is either String, Number, or Symbol, return the result of the comparison ToPrimitive(x) == y. Return false.

In above code block, x is a which is an object while y is a boolean value which has type Boolean. Hence when doing comparison a == true, it will have below execution sequence:

Evaluate step 7, this step will first convert true to 1

to 1 Next, it will evaluate step 9, it converts a to a primitive value.

to a primitive value. According to rules of ToPrimitive, a will be converted to a number finally and it will call object a's valueOf() method.

will be converted to a number finally and it will call object a's method. Since a has valueOf() defined and it will return 1 when called the first time

when called the first time After all above steps, a == true will return true since 1 == 1

ToPrimitive:

Assert: input is an ECMAScript language value. If Type(input) is Object, then If PreferredType was not passed, let hint be "default". Else if PreferredType is hint String, let hint be "string". Else PreferredType is hint Number, let hint be "number". Let exoticToPrim be ? GetMethod(input, @@toPrimitive). If exoticToPrim is not undefined, then Let result be ? Call(exoticToPrim, input, « hint »). If Type(result) is not Object, return result. Throw a TypeError exception. If hint is "default", set hint to "number". Return ?OrdinaryToPrimitive(input, hint). Return input.

And OrdinaryToPrimitive

Assert: Type(O) is Object. Assert: Type(hint) is String and its value is either "string" or "number". If hint is "string", then Let methodNames be « "toString", "valueOf" ». Else, Let methodNames be « "valueOf", "toString" ». For each name in methodNames in List order, do Let method be ? Get(O, name). If IsCallable(method) is true, then Let result be ? Call(method, O). If Type(result) is not Object, return result. Throw a TypeError exception.

Next the expression a == false will be evaluated, the steps are similar to what a == true does. However, this time when a.valueOf() is called, the value it returned will be 0. And false will also be converted to 0, a == false will return true now.

The final result will be true with above code block. By knowing how == comparison works, many other expressions seem impossible at first glance can actually be possible.

For example, there is also another famous interview question about JavaScript saying can a == 1 && a == 2 && a == 3 be true?

The answer is Yes. Just need to define an object as below:

const a = { num: 1, valueOf: function(){ return this.num++; } }

Apparently in reality we would rarely see this kind of code in our application. The purpose here is to let you know that don't be surprised if you see some weird behavior in JavaScript. And also a rule of thumb is to stick to use === whenever you can because it does the strict comparison which requires two objects have the same type.