Testing for Equality
Another topic raised during my mock JavaScript interview last month concerned the difference between two comparison operators: == and ===. My interviewer asked what the output of certain code would be depending on which of those operators was used. This is a very basic question, but there’s a good chance you’ll be asked about this in an actual JavaScript interview.
Equal in What Way?
First, what is the difference between == and ===? The first, called the equality operator, compares values. The second, called the strict equality operator, compares values and data types.
Take a look at the following code. What do you think the output will be? (Hint: think Boolean.)
let num = 75let str = “75”console.log(num == str)
The output to the console would be… true. How so? Using the typeof operator on these variables confirms that our num variable holds a number and our str variable holds a string. Why then does the equality operator show them having the same value?
And what if we use the strict equality operator instead?
let num = 75let str = “75”console.log(num === str)
The output this time is false. What accounts for these differing results?
This is due to two characteristics of JavaScript:
- JavaScript is a weakly typed language, meaning that you can assign any value to a variable without having to declare the data type.
2. JavaScript therefore allows for type coercion, by which a value of one data type can be automatically (or implicitly) converted to another data type.
Get Lyndel Barnes’s stories in your inbox
Join Medium for free to get updates from this writer.
In our first example, the equality operator converts the string “75” to a number and then makes the comparison, thereby showing that the values held in num and str are equal. In the second example, we see that the strict equality operator considers both value and data type when making the comparison; hence, it declares num and str are not equal.
So if, for instance, you see x == y in a line of code, the code is asking, “Is x equal in value to y?” If you see x === y, it’s asking, “Is x equal in value and type to y?”
Checking for Inequality
You may have encountered ! (known as the logical NOT operator) in JavaScript. As its name suggests, its purpose is to indicate logical negation, that something is not the case.
What if you encounter != or !== in JavaScript code? These check for inequality. The first is the inequality operator, and its purpose is to check for unequal values. The second, which is the strict inequality operator, checks for unequal values and unequal data types.
Let’s try this with our earlier examples.
With the inequality operator:
let num = 75let str = “75”console.log(num != str)
Our output? This time it’s false. Our code asks, “Is num not equal in value to str?” Like the equality operator, it uses type coercion to make the comparison and, finding num and str to hold equal values, returns false.
With the strict inequality operator:
let num = 75let str = “75”console.log(num !== str)
Because !==, just like ===, considers both value and data type, our code in this case returns true. The strict inequality operator asks, “Is num not equal in value and data type to str?” It will not perform type coercion like != does, so it finds our two variables are not of equal value and type, thus returning true.









