Test your JavaScript Skills: Fundamentals - 15

Part 15

Introduction

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

  1. Identify the output of the below program

     if 4>3 
         console.log("test");
    

    a.test

    b.undefined

    c.no output

    d.null

    e.error

    Answer: e

    Reason: The expression 4>3 should be in braces ().

  2. Identify the output of the below program

     if (4>3) console.log("true") else console.log("false");
    

    a.true

    b.false

    c.undefined

    d.error

    e.no output

    Answer: d

    Reason: The syntax is incorrect. The semicolon is missing for the first truthy instruction i.e. console.log("true");

  3. Identify the output of the below program

     if (true+null)
        console.log("true");
     else
        console.log("false");
    

    a.true

    b.false

    c.undefined

    d.error

    e.0

    Answer: a

    Reason: Here 'true' is coerced to 1 and null is coerced to 0 hence 1+0 is 1.

  4. Identify the output of the below program

     if (4>3) console.log("true"); else console.log("false");
    

    a.true

    b.false

    c.undefined

    d.error

    e.null

    Answer: a

    Reason: The condition is true hence result is true.

  5. Identify the output of the below program

     if (undefined+true)
        console.log("true");
     else
        console.log("false");
    

    a.NaN

    b.true

    c.false

    d.undefined

    e.error

    Answer: c

    Reason: Here 'undefined' is coerced into NaN and true to 1 hence NaN+1 is NaN

  6. Identify the output of the below program

     if ({no:10}+ undefined)    
         console.log("true");
     else
         console.log("false");
    

    a.undefined

    b.false

    c.error

    d.true

    e.null

    Answer:d

    Reason: The object is converted to a string and undefined is concatenated to a string.

  7. Identify the output of the below program

     if (4>3) console.log("true");
    

    a.true

    b.no output

    c.false

    d.error

    e.undefined

    Answer: a

    Reason: Condition is true hence result is true.

Did you find this article valuable?

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