Test your JavaScript Skills:Fundamentals-10

Part 10

Introduction

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

  1. Identify the output of the below program

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

    a.false

    b.true

    c.error

    d. undefined

    e.NaN

    Answer: a

    Reason: NaN+1 is NaN which is a falsey value hence result is false.

  2. Identify the output of the below program

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

    a.error

    b.undefined

    c.true

    d. false

    e.-Infinity

    Answer:c

    Reason: -Infinity is a truthy value.

  3. Identify the output of the below program

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

    a.error

    b.NaN

    c.false

    d. true

    e.undefined

    Answer: d

    Reason: The null is coerced into a number which is 0 hence (0 + 1) is 1, which is a truthy value.

  4. Identify the output of the below program

     if ({})  
        console.log("true"); 
     else
       console.log("false");
    

    a.undefined

    b.null

    c.true

    d.error

    e.false

    Answer: c

    Reason: An empty object is a truthy value.

  5. Identify the output of the below program

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

    a.true

    b.false

    c.error

    d. undefined

    e.1

    Answer: b

    Reason: The undefined is coerced into a number which is NaN and NaN + 1 is NaN which is falsey.

  6. Identify the output of the below program

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

    a.true

    b.false

    c.error

    d.undefined

    e.Infinity

    Answer: a

    Reason: Infinity is a number that is non-zero and it is considered to be a truthy value.

  7. Identify the output of the below program

     if ([])  
       console.log("true"); 
     else
       console.log("false");
    

    a. true

    b.error

    c.false

    d. undefined

    e.0

    Answer: a

    Reason: An empty array is an object that is a truthy value.

Did you find this article valuable?

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