Interview Question - JavaScript Strict Equality

This video covers a coding problem on JavaScript Strict Equality using triple equals (===).

Code

let value1 = 42;
let value2 = "42";
console.log((value1 == value2) === (value1 === value2))

Full Transcript

Let's look at this JavaScript equality problem and our goal is to find its output. The variable value1 holds a number 42 and another variable value2 holds a string 42 inside it. Now we want to log the resulting value of this expression, where double equals (==) compare the value1 and value2 using loose equality. And this triple equals (===) compare value1 and value2 using strict equality. We will see in a minute what loose in strict equality mean? Finally, we log the results from loose and strict equality to see if they both return the same result.

When we use the double equals to compare value1 with value2 it, implicitly convert the string 42 to number and compare them, which gives us the result true. On the other hand, using strict equality with triple equals, there is no implicit type conversion and it treats a string as a string and a number as a number. So when the quality check is performed between value1 and value2, it returns false. And finally, when true and false are checked, using triple equals strict equality, they return false, which is the actual output of this problem.