Javascript equality operator

 Javascript equality operator

Javascript provides equality operator to compare values. It is denoted by double 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 operator does not perform strict comparisions. Thus, it will return true in cases such as 5= '5' or 5 = "5" even though they are different data types.

This performs common type conversion. Thus we get these results.

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



<!DOCTYPE html>
<html>
<body>

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

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

</body>
</html>

Assignment:
Compare strings using equality operator.

Post a Comment

0 Comments

React