What’s the difference between double and triple equals operators . Javascript supports both supports strict equality (===) and type-converting equality (==).
In the beginning it’s seems confusing to me when i see comparison using triple equals. Later i know the reason why triple comparison is used.
In this post i am going to explain what’s the difference between double(==) and triple(===) equal operators.
Difference Between Double (==) And Triple (===) Equals in Javascript
Double (lenient or normal) equal to compares the two value and return true or false based on that.
Double equality (==) converts the operands to the same type before making the comparison. That’s why it is also called type-converting equality.
So comparing number with string having the same value will return true.
How to Reverse String in Javascript.
For example –
1 2 3 4 5 6 7 |
/* Return True. Both values are integer. */ console.log(8 == 8); /* return true. */ |
It returns true because both value are same. Take another example.
1 2 3 4 5 6 7 |
/* Return True. First value is string and second is integer still getting true result. */ console.log("8" == 8); /* return true. */ |
In this comparison first value is string and second value is integer. It return true value, as it typecast the first value to int.
Triple Equals
Now let’s see how Triple equal (strict comparison) works in this case. Triple equality operator compares values as well as data type.
1 2 3 |
/* Return False. */ console.log("8" === 8); |
If you compare “8” with 8 using triple equals to the result will be false. As this “8” represent string values whereas other 8 represents integer.
Example of Double (==) and Triple (===)Equal in Javascript
Let’s check some example with double and triple equal to.
1. When we compare two strings with triple equality (===), it return true when they have the same sequence of characters, same length, and same characters in corresponding positions.
2. Two numbers are strictly equal only when they are numerically equal.
Difference between single and double quotes .
3. Two Boolean operands are strictly equal if both are true or both are false.
1 2 3 4 5 6 7 8 9 10 |
/* If you compare with double equal to, result is true. */ console.log(1 == '1'); /* return true. */ /* But when we compare with triple equal to. */ console.log(1 === '1'); /* return false.*/ |
4. Comparing Objects with double (==) and triple (===) equal.
When two objects are compare using triple equal (===), it return true if and only if they refer to the same instance of the class.
5. In Javascript if we compare Null and Undefined using double equal (==) then true is return. But when we compare Null and Undefined with triple equal to (===) it return false.
Difference between Null and Undefined value.
1 2 3 4 5 6 7 |
/* Return True */ console.log(null == undefined); /* Return False */ console.log(null === undefined); |
Performance comparison of Double and Triple equal
If you are comparing two variables of same data type, both the operator takes equal time. But if the values you are comparing are of different type in that case triple equals operator will be faster because it would not try to convert the types of variable.
Conclusion
If you need strict comparison always use triple equal (===) operator.