Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is === in JavaScript

Understanding the Triple Equals (===) in JavaScript

The Basics

In JavaScript, you might have come across == and === operators. While they might seem similar, there's a significant difference between the two. Let's start by understanding ==, also known as the double equals operator.

The double equals operator == is a comparison operator, which transforms the operands to the same type before making the comparison. But what does this mean? Think of it as a translator. If you're trying to compare French to English, you'd need to translate one language to the other first. This is what == does. It converts the operands to a common type, and then makes the comparison.

Let's look at an example:

console.log(5 == "5"); // This will output: true

Even though one is a number and the other is a string, == translates them to a common type and then makes the comparison.

Enter the Triple Equals Operator (===)

Now, let's move on to the star of the show, ===, also known as the triple equals operator. The === operator is a stricter version of ==. It compares both the type and the value of its operands. This means there are no translations, no conversions. It's like comparing French to French, or English to English.

Here's the same example with ===:

console.log(5 === "5"); // This will output: false

This time, the output is false. Why? Because 5 and "5" are not the same type. One is a number, the other is a string.

Why Use === Instead of == ?

You might be thinking, "Why bother with === when == seems to work just fine?" Well, using == can sometimes lead to unexpected results because of its type coercion (translation). This can make your code behave in strange ways.

Here's an example:

console.log(true == "1"); // This will output: true
console.log(true === "1"); // This will output: false

In the first line, == translates the operands and then compares them. But in the second line, === does not do any translation. So, true (a Boolean) and "1" (a string) are considered different.

=== and Objects

When comparing objects, === can still be a bit tricky. Let's look at an example:

let car1 = {brand: "Toyota"};
let car2 = {brand: "Toyota"};

console.log(car1 === car2); // This will output: false

Even though car1 and car2 seem to be the same, the output is false. This is because === doesn't compare the contents of the objects. Instead, it checks if the objects are the same instance, much like asking, "Are these two objects the exact same object, not just identical twins?"

If we were to compare an object to itself, however:

let car1 = {brand: "Toyota"};
let car3 = car1;

console.log(car1 === car3); // This will output: true

The result is true, because car1 and car3 are references to the same instance.

Conclusion

To sum it up, === is a strict comparison operator in JavaScript that compares both the type and value of its operands. It's like an uncompromising school teacher who expects both the homework content and handwriting to be perfect. On the other hand, == is a more relaxed operator that's happy as long as the homework content is correct, even if the handwriting is a bit sloppy.

Using === can help prevent unexpected bugs in your code, making it more reliable and robust. So, the next time you're comparing values in JavaScript, remember to ask yourself: do I want a strict comparison (===), or a loose one (==)?

Happy coding!