Javascript strict equality operator

Javascript strict equality operator

Javascript provides strict equality operator to compare values and overcome the limitations of equality operator. It is denoted by triple equal ===

It returns true if the result of comparision is true, othewise it returns false.


Please note, this is not the same as assignment or simple assignment operator

This performs strict comparisions. Thus, it will not return true in cases such as 5= '5' or 5 = "5" as they are different data types. It will return false value when the types are different.

Look at the example to understand this. We are using a simple function:checkStrictEquality that accepts two parameters a, and b to test the logic.



<!DOCTYPE html>
<html>
<body>

<button onclick="checkStrictEquality(5,5)">Check if 5 = 5</button>
<button onclick="checkStrictEquality(5,2)">Check if 5 = 2</button>
<button onclick="checkStrictEquality(5,'5')">Check if 5= '5'</button>
<button onclick='checkStrictEquality(5,"5")'>Check if 5 = "5"</button>
<p id="result"></p>

<script>
function checkStrictEquality(num1num2) {
    if(num1 === num2){
        document.getElementById("result").innerHTML = "Equal";
    }
    else{
        document.getElementById("result").innerHTML = "Not equal";
    }    
    }
</script>

</body>
</html>


Post a Comment

0 Comments

React