console.log(3 == "3"); // true console.log(3 === "3"); // false.
Using two equal signs returns true because the string “3” is converted to the number 3 before the comparison is made. Three equal signs sees that the types are different and returns false. Here’s another:
console.log(true == '1'); // true console.log(true === '1'); // false
Again, the abstract equality comparison performs a type conversion. In this case both the boolean true and the string ‘1’ are converted to the number 1 and the result is true. Strict equality returns false.
If you understand that you are well on your way to distinguishing between == and ===. However, there’s some scenarios where the behavior of these operators is non intuitive. Let’s take a look at some more examples:
console.log(undefined == null); // true console.log(undefined === null); // false. Undefined and null are distinct types and are not interchangeable. console.log(true == 'true'); // false. A string will not be converted to a boolean and vice versa. console.log(true === 'true'); // false
The example below is interesting because it illustrates that string literals are different from string objects.
console.log("This is a string." == new String("This is a string.")); // true console.log("This is a string." === new String("This is a string.")); // false
To see why strict equality returned false, take a look at this:
console.log(typeof "This is a string."); // string console.log(typeof new String("This is a string.")); //object
The new operator will always return an object and you will get the same results when comparing primitive numbers and booleans to their respective object wrappers.
Speaking of objects, what happens if we want to compare reference types? Do abstract and strict comparison behave any differently when we are dealing with objects? Yes! There is another rule you need to keep in mind. When comparing reference types both abstract and strict comparisons will return false unless both operands refer to the exact same object. Consider the following:
var a = ; var b = ; var c = a; console.log(a == b); // false console.log(a === b); // false console.log(a == c); // true console.log(a === c); // true
Even though a and b are of the same type and have the same value, both abstract and strict equality return false.
So which one should I use?
Keep it strict. Using the strict equality operator by default will increase the clarity of your code and prevent any false positives caused by abstract equality comparison. When you need to compare values of different types, do the conversions yourself. The more explicit your code, the better.
For more on this topic, take a look at the ECMAScript Language Specification. Also check out this nifty table showing all possible type comparisons. Finally, see this post by mozilla for a comprehensive discussion of equality and sameness.
5 keys to successful organizational design
How do you create an organization that is nimble, flexible and takes a fresh view of team structure? These are the keys to creating and maintaining a successful business that will last the test of time.Read more
8 ways to stand out in your stand-up meetings
Whether you call them stand-ups, scrums, or morning circles, here's some secrets to standing out and helping everyone get the most out of them.Read more
Technology in 2025: Prepare your workforce
The key to surviving this new industrial revolution is leading it. That requires two key elements of agile businesses: awareness of disruptive technology and a plan to develop talent that can make the most of it.Read more