One of favorite interview question on JavaScript which I normally get started with. And it turns out the answer I knew, and in fact invariable everyone who gave the answer, were wrong.

So the answer I expected is

== check the

valueand === check bothvalue and type

Well it turns out although from the behavior or output of it it seems correct but in reality it’s actually not what is happening.

I happen to listen to Kyle Simpson at his talk New Rules For JavaScript and he explain very well, why we are wrong when we consider the above statement.

So what happens really when a comparison operator is called in JS.

=== is known as Strict Equality Comparison and the algorithm it follows is below

- If Type(
*x*) is different from Type(*y*), return**false**. - If Type(
*x*) is Undefined, return**true**. - If Type(
*x*) is Null, return**true**. - If Type(
*x*) is Number, then- If
*x*is**NaN**, return**false**. - If
*y*is**NaN**, return**false**. - If
*x*is the same Number value as*y*, return**true**. - If
*x*is**+0**and*y*is**−0**, return**true**. - If
*x*is**−0**and*y*is**+0**, return**true**. - Return
**false**.

- If
- If Type(
*x*) is String, then- If
*x*and*y*are exactly the same sequence of characters (same length and same characters in corresponding positions), return**true**. - Else, return
**false**.

- If
- If Type(
*x*) is Boolean, then- If
*x*and*y*are both**true**or both**false**, return**true**. - Else, return
**false**.

- If
- If
*x*and*y*are the same Symbol value, return**true**. - If
*x*and*y*are the same Object value, return**true**. - Return
**false**.

Where as == or the Abstract Equality Comparator follows the algorithm listed below:

- If Type(
*x*) is the same as Type(*y*), then- Return the result of performing Strict Equality Comparison
*x*===*y*.

- Return the result of performing Strict Equality Comparison
- 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 or Number 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 or Number,

return the result of the comparison ToPrimitive(*x*) ==*y*. - Return
**false**.

There are two things to notice here:

- === is not really doing any type checking. Where as == is actually doing type conversion.
- As opposed to common belief == actually do more work than ===

According to this test result at jsPerf.com almost in all browsers == takes more time than === although marginally.

So essentially the correct answer to the above question show be:

== does type conversion when comparing whereas === do not do it.

In other words

== allow coercion and === do not allow it.