Test your JavaScript Skills : Double not operator

Part 23

Introduction

Namaste, In this blog I will discuss 10 code scenarios on double not operator.

  1. Identify the output of the below program

     let v = !!10;
     console.log(v);
    

    a. true

    b. false

    c. 10

    d. undefined

    e. null

    Answer: a

    Reason: The double not operator for a non-zero number is true

  2. Identify the output of the below program

     let v = !!"JS";
     console.log(v);
    

    a.false

    b.JS

    c.undefined

    d.null

    e.true

    Answer:e

    Reason: The double not operator for a non-empty string is true

  3. Identify the output of the below program

     let v = !!"  ";
     console.log(v);
    

    a.true

    b.false

    c.undefined

    d.null

    e.error

    Answer: a

    Reason: The double not operator for a non-empty (even spaces) string is true

  4. Identify the output of the below program

     let v = !!"";
     console.log(v);
    

    a.false

    b.true

    c.undefined

    d.null

    e.error

    Answer: a

    Reason: The double not operator for an empty string is false

  5. Identify the output of the below program

     let v = !!null;
     console.log(v);
    

    a.null

    b.undefined

    c.true

    d.false

    e.error

    Answer:d

    Reason: The double not operator for null is false

  6. Identify the output of the below program

     let v = !!NaN;
     console.log(v);
    

    a.undefined

    b.NaN

    c.true

    d.false

    e.error

    Answer:d

    Reason: The double not operator for NaN is false

  7. Identify the output of the below program

     let v = !!Infinity;
     console.log(v);
    

    a.false

    b.true

    c.Infinity

    d.null

    e.error

    Answer: b

    Reason: The double not operator for Infinity is true

  8. Identify the output of the below program

     let v = !!-Infinity;
     console.log(v);
    

    a.false

    b.true

    c.Infinity

    d.null

    e.error

    Answer: b

    Reason: The double not operator for -Infinity is true

  9. Identify the output of the below program

     let v = !!0;
     console.log(v);
    

    a.false

    b.true

    c.Infinity

    d.null

    e.error

    Answer: a

    Reason: The double not operator for 0 is false

  10. Identify the output of the below program

    let v = !!{};
    console.log(v);
    

    a.false

    b.true

    c.Infinity

    d.null

    e.error

    Answer: b

    Reason: The double not operator for the empty object is true

Did you find this article valuable?

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