JavaScript Double Equals Vs Triple Equals (== vs ===)

JavaScript Double Equals Vs Triple Equals (== vs ===) | This article describes the distinction between == and === for beginners in JavaScript. Equality checking is a common task for programmers in all programming languages. Whether or whether this value is equivalent to that value.

Double equals (==) and triple equals (===) are two equality-checking operators in JavaScript. Additionally, they frequently confound programmers when used. Well, understanding them isn’t that difficult.

We’ll discuss several vital distinctions and application scenarios in this post, including when and how to effectively employ each of the two types of operators. And what’s this? It’s amusing to learn about them as well.

Following the execution, all comparison operators yield Boolean results. True or false, depending. There are only two values in programming, as we are all aware: 1 and 0. Therefore, if we continue, true will become 1, and false will become 0. Okay, let’s begin this while keeping that in mind.

JavaScript offers two options for comparison:-

  • Comparison with the type
  • Comparison of values

In JavaScript, you might have seen the double and triple-equals symbols. But what do they mean? In essence, == transform type automatically, whereas === does not.

Double equals check only value equality (==). It automatically engages in coercion. This indicates that the types of variables are changed to match each other before checking the values.

However, Triple Equals (===) doesn’t engage in type coercion. It will check to see if the variables being compared have the same type AND value.

Let’s use a few instances to comprehend the distinction better. Think about the results that each of these statements will produce. Also see:- JavaScript Operator Precedence

Example-1:- Demonstrate JavaScript Double Equals Vs Triple Equals (== vs ===) for String

const str1 = "hello" 
const str2 = "hello"  

console.log(str1 == str2) 
console.log(str1 === str2) 

Output:-

true
true

Example-2:- Demonstrate JavaScript Double Equals Vs Triple Equals (== vs ===) for Number & String

Both str1 and str2 have the same value and type. The outcome is valid for both as a consequence.

const num = 8734;
const stringNum = "8734";

console.log(num == stringNum);
console.log(num === stringNum);

Output:-

true
false

Here, the values of num and stringNum resemble one another. The types of numbers are num and string respectively. The type is different even if the values are the same. Therefore, when checking for value and type, the == comparison returns true, but the === gives false.

Example-3:- Demonstrate JavaScript Double Equals Vs Triple Equals (== vs ===) for Number & Boolean

console.log(0 == false);
console.log(0 === false);

Output:-

true
false

This is a case worth exploring. When false is checked, the value of 0 remains the same. The reason is that although 0 and false have the same value in JavaScript when type and value are limited, the === value is false since 0 is a number, and false is a boolean.

Example-4:- Demonstrate JavaScript Double Equals Vs Triple Equals (== vs ===) for String & Boolean

const str = "";

console.log(str == false);
console.log(str === false);

Output:-

true
false

In JavaScript, the values of an empty string and a false are identical. So, == gives a true result. The type is different, though, and so === returns false.

When should we use JavaScript Double Equals Vs Triple Equals (== vs ===) Operator?

  • Use === if you’re unsure. You will avoid a tonne of potential bugs if you do this.
  • Use == if you provide a use case where it is okay to be a little lax with the incoming data type.

Use ==, for instance, if an API accepts “true” and true from the client. Unless you have a compelling use case, avoid using ==.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *