Test your JavaScript Skills:Fundamentals-9

Photo by RetroSupply on Unsplash

Test your JavaScript Skills:Fundamentals-9

Part 9

Introduction

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

  1. Identify the output of the below program

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

    a.true

    b.false

    c.error

    d. undefined

    e.NaN

    Answer: b

    Reason: Here undefined is a false value.

  2. Identify the output of the below program

     if(-1)  
         console.log("true");
     else
         console.log("false");
    

    a.error

    b.undefined

    c.NaN

    d. true

    e.false

    Answer: d

    Reason: -1 is a truthy value.

  3. Identify the output of the below program

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

    a.NaN

    b.true

    c.false

    d. undefined

    e.error

    Answer: c

    Reason: null is a falsey value.

  4. Identify the output of the below program

     if(2)  
           console.log("true");
     else
           console.log("false");
    

    a.false

    b.true

    c. undefined

    d. NaN

    e.error

    Answer: b

    Reason: 2 is a truthy value.

  5. Identify the output of the below program

     if(0)  
           console.log("true");
     else
           console.log("false");
    

    a.false

    b.true

    c.NaN

    d. Error

    e.undefined

    Answer: a

    Reason: 0 is a falsey value.

  6. Identify the output of the below program

     let str = "ram";
     if(str)  
         console.log("true"); 
     else
       console.log("false");
    

    a.false

    b.NaN

    c.undefined

    d. true

    e.error

    Answer: d

    Reason: A string is a truthy value.

  7. Identify the output of the below program

    
     if(NaN)  
       console.log("true");
     else
       console.log("false");
    

    a.false

    b.true

    c.NaN

    d. undefined

    e.error

    Answer: a

    Reason: NaN is a falsey value.