JavaScript : Logical Operators

What are Logical Operators ?

Logical operators are used for creating compound expressions i.e using logical operators the programmer can combine expressions to perform conditional executions.

In JavaScript the common logical operators are

  • AND : && - The Compound expression is true when all the expressions are true

  • OR : || - The Compound expression is true when any of the expressions is true

  • NOT : ! - It negates the given expression

Using Logical Operators

Using the || (OR) operator

  • Case 1

      if (10-10 || 20-20) // 0
        console.log("true");
      else
        console.log("false");  //false
    

    In the above code , the compound expression yields into 0 which is false.

  • Case 2

      if (10-10 || 20-21) // -1
        console.log("true"); // true
      else
        console.log("false");
    

    In the above code, the compound expression yields into -1 which is true.

  • Case 3

      if (console.log("if")|| console.log("if2")) // if, if2
          console.log("true"); 
       else
        console.log("false"); // false
    

    In the above code, the compound expression yields into undefined which is false.

  • Case 4

      let a=20;
      if (console.log(a=a+5) || console.log(a=a+5)) // prints - 25 30 
        console.log("true - a:", a);
      else
        console.log("false -a",a);   // false - 30
    

    In the above code, the compound expression yields into undefined which is false

  • Case 5

      // first truthy value or last value
      let res = NaN || undefined || 0 || null  ; // null
      let res2 = NaN || undefined || 0  ; // 0
      let res3 = NaN || undefined ; // undefined 
      let res4 = NaN || undefined || -1 || null; // -1
      console.log(res,res2,res3,res4); // null, 0, undefined, -1
    

    In the above code , the first truthy value is taken or the last value.

  • Case 6

      false || null || console.log("JS");  // console evaluation
    

    In above code, since the first two expressions are falsey hence 'JS' is printed

  • Case 7

      function fx() {
          return 10;
      }
    
      function fx2() {
          return 20;
      }
    
      console.log(fx() || fx2()); // 10
    

    In the above code the first expression (call to fx) is truthy hence 10 is printed

Using the &&(AND) Logical operator

  • Case 1

      if (console.log("if") && console.log("if2")) // if
          console.log("true"); 
      else
        console.log("false"); // false
    

    In the above code , the compound expression yields into undefined which is false.

  • Case 2

      // First falsey value or last truthy value
      let res = null && undefined && NaN // null
      let res2 = 10 && 20 && 30; // 30
      let res3 = 10 && 20; // 20
      console.log(res,res2,res3); // null,  30, 20
    
  • Case 3

      let x=1;
      (x>2) && console.log("first"); console.log("second"); console.log("third"); 
      // second , third not part of condition
    

    In the above code, the condition 1>2 is false hence it prints second, third.

  • Case 4

      function fx() {
          return 10;
      }
      function fx2() {
          return 20;
      }
      console.log(fx() && fx2()); // 20
    

    In the above, the compound expression yields into truthy hence prints 20.

Complete Code Listing

//Objective: Using Logical operators
//Author: Mahavir

// OR - operator
if (10-10 || 20-20) // 0
  console.log("true");
else
  console.log("false");  //false

if (10-10 || 20-21) // -1
  console.log("true"); // true
else
  console.log("false"); 

if (console.log("if")|| console.log("if2")) // if, if2
    console.log("true"); 
 else
  console.log("false"); // false

let a=20;
if (console.log(a=a+5) || console.log(a=a+5)) // prints - 25 30 
  console.log("true - a:", a);
else
  console.log("false -a",a);   // false - 30

// first truthy value or last value
let res = NaN || undefined || 0 || null  ; // null
let res2 = NaN || undefined || 0  ; // 0
let res3 = NaN || undefined ; // undefined 
let res4 = NaN || undefined || -1 || null; // -1
console.log(res,res2,res3,res4); // null, 0, undefined, -1

false || null || console.log("JS");  // console evaluation

function fx() {
    return 10;
}

function fx2() {
    return 20;
}

console.log(fx() || fx2()); // 10

if (console.log("if") && console.log("if2")) // if
    console.log("true"); 
else
  console.log("false"); // false

// AND operator
// First falsey value or last truthy value
res = null && undefined && NaN // null
res2 = 10 && 20 && 30; // 30
res3 = 10 && 20; // 20
console.log(res,res2,res3); // null,  30, 20

let x=1;
(x>2) && console.log("first"); console.log("second"); console.log("third"); 
// second , third not part of condition

console.log(fx() && fx2()); // 20

Did you find this article valuable?

Support Programming with Mahavir by becoming a sponsor. Any amount is appreciated!