Checkout this video:
“==” checks for value equality
The == operator performs a type conversion before checking for value equality, while the === operator does not. This means that, in order for == to return true, the two values must first be converted to the same type. However, === will only return true if the two values have both the same type and value.
For simple types (like numbers), this is usually not an issue — but for more complex types (like objects), it can definitely lead to some unexpected results:
let x = 5;
let y = “5”;
console.log(x == y); //true
console.log(x === y); //false
Overall, it’s always best to use === unless you have a specific reason to use ==. With ===, you avoid any potential weirdness that might pop up due to type conversion — and you can sleep soundly knowing that your comparisons are being done correctly.
“===” checks for value and type equality
The == operator checks for value equality, meaning that it will convert different types of values to see if they are equal. For example, the string “1” is equal to the number 1. This can sometimes be useful, but it can also lead to unexpected results.
The === operator checks for both value and type equality, meaning that it will not convert different types of values. This can avoid some problems, but it also means that === may not behave as you expect.
“===” is known as the strict equality operator and it does not convert data type while comparing two values, on the other hand “==” is known as the equality operator and it converts the data types of values before comparing them. That is why “===” is always recommended over “==”.
It avoids type coercion
For example, let’s say you have two variables:
var a = 1;
var b = “1”;
In order to get around this issue, you can use the “===” (triple-equals) comparison operator instead. This operator does not do any type coercion, so it will return false if the two values being compared are not of the same type. In our example above, the expression a === b would return false.
It makes your code more readable
This might not seem like a big deal, but it can actually make a big difference in the readability of your code. When you use the == operator, you are implicitly telling the reader of your code that you are aware that the two values might not be of the same type, but that you don’t care about that distinction. On the other hand, when you use the === operator, you are telling the reader of your code that you do care about the distinction between different types.
“5” == 5 //true
“5” === 5 //false
When you want to check for value equality
The main difference is that == will try to convert (coerce) the values being compared to the same type before comparing, while === does not do any type conversion. This can lead to some unexpected results, as we’ll see below.
Double equals (==) will convert the types before comparing:
-If both values are numbers, then it will compare their numeric values.
-If both values are strings, then it will compare them alphabetically.
-If one value is a boolean, it will convert it to 1 if true or 0 if false before comparing.
When you want to check for value and type equality
The double equals == operator
The triple equals === operator
You might think that these two operators would work exactly the same way, but they don’t. In fact, they perform two completely different operations.
The === operator, on the other hand, will not convert the values to the same type before checking for equality. This means that the values 5 and “5” are NOT equal when using the === operator because they are of different types.