Test your JavaScript Skills:Fundamental-22

Part 22

Introduction

Namaste, In this blog I will discuss 7 code scenarios on Javascript fundamentals.

  1. Identify the output of the below program

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

    a.first

    b.second

    c.no output

    d.error

    e.undefined

    Answer:b

    Reason: The condition is false and the 2nd console.log is not part of the expression hence "second" is printed.

  2. Identify the output of the below program

     0;
     1;
     console.log("2");
    

    a.0

    b.1

    c.2

    d.error

    e.undefined

    Answer:c

    Reason: The program is syntactically correct hence 2 is printed.

  3. Identify the output of the below program

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

    a.first

    b.1

    c.2

    d.second

    e.error

    Answer: d

    Reason: The first expression of the || operator is falsey hence the second expression is taken, and therefore "second" is printed.

  4. Identify the output of the below program

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

    a.1

    b.undefined

    c.error

    d.20

    e.10

    Answer:d

    Reason: Both the functions return truthy values and since && is used the last value is taken which is 20.

  5. Identify the output of the below program

     let a = +"3";
     console.log (typeof a);
    

    a.string

    b.number

    c.NaN

    d.undefined

    e.null

    Answer:b

    Reason: The string is coerced into a number.

  6. Identify the output of the below program

     x=10;
     (x>2) && console.log("first") || console.log("second");
    

    a.first

    b.second

    c.10

    d.2

    e.error

    Answer:b

    Reason: The first expression of the || operator is falsey hence the second expression is taken, and therefore "second" is printed.

  7. Identify the output of the below program

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

    a.10

    b.20

    c.0

    d.error

    e.undefined

    Answer: a

    Reason: The call to fx() returns truthy value hence 10 is printed.

Did you find this article valuable?

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