Difference between double equal vs triple equal JavaScript
Last Updated :
06 Jan, 2023
Double equal: The double equal(‘==’) operator tests for abstract equality i.e. it does the necessary type conversions before doing the equality comparison.
Triple equal: The triple equal(‘===’) operator tests for strict equality i.e it will not do the type conversion hence if the two values are not of the same type, when compared, it will return false.
Example 1: In this example, we will check abstract and strict quality. One will return true between a string 9 and number 9. Because there is no type comparison, in the case of ‘===’ it will return false.
Javascript
<script>
console.log(9 == "9" );
console.log(9 === "9" );
</script>
|
Output:
true
false
We have an article on JavaScript ‘===’ vs ‘==’ Comparison Operator, you can go through that article for in-depth information.
Example 2: Here L.H.S. is a string literal whereas R.H.S. is a string object, due to the type conversion of a string object into a string literal, it returns true.
Javascript
<script>
console.log( "GeeksforGeeks" == new String( "GeeksforGeeks" ));
console.log( "GeeksforGeeks" === new String( "GeeksforGeeks" ));
</script>
|
Output:
true
false
Example 3: Here number 1 is converted into true(boolean type) in javascript true is referred to as 1 and false is referred to as 0, hence it returns true.
Javascript
<script>
console.log( true == '1' );
console.log( true === '1' );
</script>
|
Output:
true
false
In general “===” operator is recommended since it never does type conversion we are doing an exact comparison thus it always produces correct results.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...