Test your JavaScript Skills: Fundamentals-11

Photo by Andrew Neel on Unsplash

Test your JavaScript Skills: Fundamentals-11

Part 11

Introduction

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

  1. Identify the output of the below program

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

    a.false

    b.true

    c.error

    d. undefined

    e.null

    Answer: b

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

  2. Identify the output of the below program

     if ([1,2,3])  
         console.log("true"); 
     else
         console.log("false");
    

    a.false

    b.true

    c.error

    d. undefined

    e.null

    Answer: b

    Reason: An array object is a truthy value.

  3. Identify the output of the below program

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

    a.null

    b.error

    c.false

    d. undefined

    e.true

    Answer: e

    Reason: A function is an object and an object is a truthy value.

  4. Identify the output of the below program

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

    a.error

    b.null

    c.undefined

    d. false

    e.true

    Answer: e

    Reason: An object is a truthy value.

  5. Identify the output of the below program

     if (new Date())  
         console.log("true"); 
     else
         console.log("false");
    

    a.undefined

    b.error

    c.false

    d. true

    e.date

    Answer: d

    Reason: An object is a truthy value.

  6. Identify the output of the below program

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

    a.error

    b.false

    c.true

    d. undefined

    e.0

    Answer: b

    Reason: The function call returns undefined which is falsey.

  7. Identify the output of the below program

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

    a.undefined

    b.null

    c.true

    d. false

    e.error

    Answer: c

    Reason: An object is a truthy value.

Did you find this article valuable?

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