Test your JavaScript Skills: Logical Operators-27

Part 27

Table of contents

Introduction

Namaste, In this blog I will discuss 10 code scenarios on JavaScript Logical Operators.

  1. Identify the output of the below program

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

    a.true

    b.false

    c.0

    d.null

    e.error

    Answer: b

    Reason: The compound expression computes to 0 which is false.

  2. Identify the output of the below program

     let a=20;
     if (console.log(a=a+5) || console.log(a=a+5)) 
       console.log("a:", a);
     else
       console.log("a",a);
    

    a.25,30

    b.30,30,30

    c.25,30,35

    d.25,30,30

    e.30

    Answer:d

    Reason:The console.log function prints 25,30. The compound expression yields into undefined which is false hence it prints 30.

  3. Identify the output of the below program

     let res = NaN || undefined || 0 || null; 
     console.log(res);
    

    a.NaN

    b.undefined

    c.0

    d.null

    e.error

    Answer:d

    Reason:The compound expression is Falsey

  4. Identify the output of the below program

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

    a.if,if2,false

    b.if,if2,true

    c.if,if2

    d.if,false

    e.if2,true

    Answer:a

    Reason:Both the console.log are printed. The compound expression returns undefined which is false.

  5. Identify the output of the below program

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

    a.0

    b.-1

    c.true

    d.false

    e.null

    Answer:c

    Reason:The compound expression yields into -1 which is true

  6. Identify the output of the below program

     let res2 = NaN || undefined || 0  ; 
     console.log(res2);
    

    a.0

    b.undefined

    c.NaN

    d.error

    e.null

    Answer:a

    Reason:The compound expression returns 0.

  7. Identify the output of the below program

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

    a.20

    b.10

    c.null

    d.undefined

    e.error

    Answer:b

    Reason:The first Truthy value is 10.

  8. Identify the output of the below program

     let res = NaN || undefined || -1 || null; 
     console.log(res);
    

    a.NaN

    b.undefined

    c.-1

    d.null

    e.error

    Answer:c

    Reason:The value -1 is Truthy.

  9. Identify the output of the below program

     let res = null && undefined && NaN 
     console.log(res)
    

    a.undefined

    b.NaN

    c.null

    d.error

    e.0

    Answer:c

    Reason:The first Falsey value is taken.

  10. Identify the output of the below program

    let x=1;
    (x>2) && console.log("first"); console.log("second"); console.log("third");
    

    a.first

    b.second

    c.third

    d.first, third

    e.second, third

    Answer:e

    Reason:The compound expression is falsey. Hence the output is second, third.

Did you find this article valuable?

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