6 Example(s) of JavaScript Logical operators


Description :

Logical Operator <, > evaluates the expression and return true or false.


JavaScript Logical operators Example - 1
<!DOCTYPE html>
<html>
 <head>
  
</head>
  <body>
<p id="sample"></p>
<script>
var x = 9;
var y = 3;

document.getElementById("sample").innerHTML = 
(x < 10 && y > 1) + "<br>" + 
(x < 10 && y < 1);
</script>
      </body>
</html>

Output

Description :

Logical Operator === will check for equality using equal value and equal type and return true and false.


JavaScript Logical operators Example - 2
<!DOCTYPE html>
<html>
 <head>
  
</head>
  <body>
<p id="sample"></p>
<script>
var x = 6;
var y = 3;

document.getElementById("sample").innerHTML = 
(x === 5 || y === 5) + "<br>" + 
(x === 6 || y === 5) + "<br>" + 
(x === 6 || y === 3);
</script>
      </body>
</html>

Output

Description :

Logical Operator === and > example


JavaScript Logical operators Example - 3
<!DOCTYPE html>
<html>
 <head>
  
</head>
  <body>
<p id="sample"></p>
<script>
var x = 6;
var y = 3;

document.getElementById("sample").innerHTML = 
!(x === y) + "<br>" + 
!(x > y);
</script>
      </body>
</html>

Output

Description :

Logical variables are used with boolean values and will return the boolean result. See the code snippet:


JavaScript Logical operators Example - 4
<!DOCTYPE html>
<html>
 <head>
 <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p id="sample"></p>
<script>
var val1 = 9;
var val2= 3;

document.getElementById("sample").innerHTML = 
(val1 < 10 && val2 > 1) + "<br>" + 
(val1 < 10 && val2 < 1);
</script>
      </body>
</html>

Output

Description :

Here is a code which use || operator (val1 === 5 || val2 === 5) will result in false and (val1 === 6 || val2 === 5) will result in true. See the code snippet:


JavaScript Logical operators Example - 5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p id="sample"></p>
<script>
var val1 = 6;
var val2 = 3;

document.getElementById("sample").innerHTML = 
(val1 === 5 || val2 === 5) + "<br>" + 
(val1 === 6 || val2 === 5) + "<br>" + 
(val1 === 6 || val2 === 3);
</script>
      </body>
</html>

Output

Description :

Example of JavaScript Logical Operator:


JavaScript Logical operators Example - 6
<!DOCTYPE html>
<html>
 <head>
 <title>Welcome to LearnKode - A code learning platform</title>
</head>
  <body>
<p id="sample"></p>
<script>
var val1 = 6;
var val2 = 3;

document.getElementById("sample").innerHTML = 
!(val1 === val2) + "<br>" + 
!(val1 > val2);
</script>
      </body>
</html>

Output