Open In App

Difference between double equal vs triple equal JavaScript

Last Updated : 06 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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>
    // In R.H.S. string "9" is converted into
    // number 9, hence returns true.
    console.log(9 == "9"); 
       
    // Here no type of conversion takes place,
    // hence returns false
    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>
    // type conversion takes place
    console.log("GeeksforGeeks" == new String("GeeksforGeeks"));
      
    // No type of conversion takes place
    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>
    // type conversion
    console.log(true == '1');
      
    // No type conversion so it returns false    
    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
Previous
Next
Share your thoughts in the comments

Similar Reads