Test your JavaScript Skills:Fundamentals-21

Photo by Carlos Muza on Unsplash

Test your JavaScript Skills:Fundamentals-21

Part 21

Introduction

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

  1. Identify the output of the below program

     let res = NaN || undefined || 0; 
     console.log("value",res);
    

    a.NaN

    b.undefined

    c.0

    d.no output

    e.error

    Answer: c

    Reason: All are falsey values hence the last value is taken which is 0.

  2. Identify the output of the below program

     let res = 10 && 20 && 30 && 40;
     console.log("res", res);
    

    a.10

    b.20

    c.30

    d.40

    e.error

    Answer:d

    Reason: The last value is taken as all values are truthy.

  3. Identify the output of the below program

     let res = !undefined;
     console.log("res", res);
    

    a.undefined

    b.error

    c.false

    d.true

    e.no output

    Answer:d

    Reason:The not of undefined is true because undefined is falsey value.

  4. Identify the output of the below program

     let res = NaN || undefined || -1 || null; 
     console.log("value", res);
    

    a.NaN

    b.null

    c.undefined

    d.-1

    e.error

    Answer: d

    Reason: The first truthy value is taken which is -1.

  5. Identify the output of the below program

     undefined || false || null || console.log("Namaste");
    

    a. false

    b.null

    c.Namaste

    d.undefined

    e.error

    Answer: c

    Reason: All are falsey values hence last instruction is executed which prints Namaste.

  6. Identify the output of the below program

     let res = null && undefined && NaN;
     console.log("value", res);
    

    a.NaN

    b.undefined

    c.null

    d.error

    e.no output

    Answer:c

    Reason: The first falsey value is taken hence the output is null.

  7. Identify the output of the below program

     undefined || false || -2 || console.log("Namaste");
    

    a. no output

    b. Namaste

    c.undefined

    d.false

    e.-2

    Answer: a

    Reason: The value -2 is truthy (taken) but it is not printed.

Did you find this article valuable?

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