JavaScript logical OR operator

 JavaScript logical OR operator

In JavaScript we use the logical OR operator to check if one of the condition is true.
It is denoted by the || symbol.

This is very to check if one of the conditions are met before executing a block of code. if one of the  conditions is 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>

<button onclick='logicalOr("Sam""Harry")'>Check if name is Harry or Sam </button>
<button onclick='logicalOr("Harry""Sam")'>Check if name is Harry or Sam </button>
<button onclick='logicalOr("Harry""Zen")'>Check if name is Harry or Sam </button>

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

    var x = document.getElementById("result");
function logicalOr(name1,name2) {
    if(name1=="Harry" || name2 =="Sam"){
        x.innerHTML = "Mactching names " + true;
    } else{
        x.innerHTML = "Name not found in database " + false;


Make it fail for both the conditions.

Post a Comment