JavaScript logical AND operator

 JavaScript logical AND operator

In JavaScript we use the logical AND operator to check if multiple conditions are true.
It is denoted by the && symbol.

This is very useful when comparing two or more conditions. Only if both of the conditions are true, the statements within that condition block are executed.

We can achieve the same results by nesting if statements. However, that'd be tedious and add up to more lines of code. Check this example.




<!DOCTYPE html>
<html>
<body>

<button onclick="usingIf()">Using nested if a is 5 and b is 15 </button>
<button onclick="logicalAnd(25,15)">Check if a is 5 and b is 15</button>

<p id="result"></p>

<script>
    var x = document.getElementById("result");
function usingIf() {
    var a = 5var b = 15;
    if(a == 5){
        ifb == 15 ){
            x.innerHTML = true;
        }   
    } else{
        x.innerHTML = false;
    }
    }

function logicalAnd(a,b) {
    if(a==5 && b==15){
        x.innerHTML = true;
    } else{
        x.innerHTML = false;
    }
    }
</script>

</body>
</html>



Post a Comment

0 Comments

React