Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is the difference between == and === in JavaScript

Getting Started with == and === in JavaScript

If you're learning to code in JavaScript, you've probably come across the double equals == and triple equals === operators. At first glance, they might seem identical. After all, both are used to compare two variables. However, they work differently under the hood and understanding these differences is crucial for writing correct and efficient code.

The Double Equals Operator (==)

In JavaScript, the double equals operator == is used to compare two variables for equality. However, unlike many other programming languages, JavaScript's == operator performs something called type coercion before making the comparison.

Type coercion, to put it simply, is the automatic or implicit conversion of values from one data type to another. For example, JavaScript might convert a string to a number, or a number to a string, depending on the context.

Let's look at an example:

var a = "5";
var b = 5;
console.log(a == b);  // Output: true

Despite one being a string and the other being a number, the == operator returns true because it converts "5" to a number before making the comparison.

The Triple Equals Operator (===)

On the other hand, the triple equals operator === in JavaScript, also known as the strict equality operator, does not perform type coercion. It compares both the value and the type of the two variables. This means that if the variables are of different types, === will return false, even if the values are the same.

Let's use the same example as before:

var a = "5";
var b = 5;
console.log(a === b);  // Output: false

This time, the output is false because "5" and 5 are of different types (string and number, respectively).

Understanding the Difference Through an Analogy

Think of == and === as two different types of interviewers. The == operator is like an interviewer who cares more about your skills than your background. They're willing to accept a self-taught programmer who has the skills, even if they don't have a degree in Computer Science.

On the other hand, === is like an interviewer who requires a specific degree. It doesn't matter how good your skills are, if you don't have the right degree, you're not getting the job.

When to Use == vs. ===

Given these differences, you might wonder when to use == and when to use ===. As a general rule of thumb, it's safer to use === because it avoids unexpected results due to type coercion.

For example, consider the following:

console.log(true == "1");     // Output: true
console.log(true === "1");    // Output: false

With ==, JavaScript converts the boolean true to 1 and the string "1" to 1, leading to an unexpected result. With ===, this confusion is avoided.

However, there might be cases where you intentionally want to take advantage of type coercion. In such cases, using == would be appropriate.

Conclusion

In the world of JavaScript, == and === are like twin siblings with different personalities. They may look similar, but they behave differently. One is more relaxed, converting types to make things match, while the other is strict, requiring both value and type to match. Understanding these differences will enable you to navigate the JavaScript landscape more efficiently, just like knowing the personalities of two twins would help you interact with them more effectively. So, remember the strict twin === and the relaxed twin == next time you write your JavaScript code!