Logical Operators in JavaScript

Logical Operators in JavaScript

Logical Operators

 Logical Operators is used for applying two or more condition at a time in a single condition are used

For example if a condition is true then it will return a statement, if a condition false out of the condition.

There are three types of Logical Operators:

&&AND
||OR
!NOT

 

AND operator :

it runs when the both conditions are true then it will returns true otherwise false

If (Condition1  && Condition1)

{

Statement;

}

Example:

<!DOCTYPE html>
<html>
<body>
  <script>
       var age =20;
        if (age>=18 && age<=21)
       {
      document.write(" You are eligible");
       }
    </script>
</body>
</html>

Output: You are eligible

Example:

<!DOCTYPE html>
<html>
<body>
  <script>
            var age=22;
           if (age> 18 && age<21)
            {
           document.write(" You are eligible");
            }

      </script>
</body>
</html>

Output: NoneO

OR Operator

It returns true when any one condition is true it is true.

If (Condition1  || Condition1)

{

Statement;

}

Example:

<!DOCTYPE html>
<html>
    <body>
      <script>
         var age=22;
         if (age> 18 || age<21)
         {
         document.write(" You are eligible");
         }
      </script>
  </body>
</html>

Output: You are eligible

Example :

<!DOCTYPE html>
<html>
 <body>
    <script>
     var age=22;
     if (age> 23 || age<21)
     {
      document.write(" You are eligible");
      }
   </script>
 </body>
</html>

Output: none

NOT Operator

It returns true when condition false, when condition false it returns true

If (! Condition)

{

Statement;

}

Example:

<!DOCTYPE html>
<html>
 <body>
     <script>
    var a=20;
    if (!a>=12)
    {
     document.write(" You are eligible");
     }
    </script>
 </body>
</html>

Output : true

Example:

<!DOCTYPE html>
<html>
<body>
      <script>
       var a=20;
       if (!a<12)
        {
         document.write(" You are eligible");
        }
      </script>
   </body>
</html>

Output: true

You are eligible