Test your JavaScript Skills : Boolean() function

Part 24

Introduction

Namaste, In this blog I will discuss 10 code scenarios on the Boolean() function.

  1. Identify the output of the below program

     let v = Boolean(undefined);
     console.log(v);
    

    a.true

    b.false

    c.undefined

    d.null

    e.error

    Answer: b

    Reason: The boolean() function for undefined returns false

  2. Identify the output of the below program

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

    a.true

    b.false

    c.null

    d.undefined

    e.error

    Answer: a

    Reason: The boolean() function for 10 returns true

  3. Identify the output of the below program

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

    a.false

    b.true

    c.null

    d.undefined

    e.error

    Answer: a

    Reason: The boolean() function for null returns false

  4. Identify the output of the below program

     let v = Boolean([]);
     console.log(v);
    

    a.[]

    b.{}

    c.true

    d.false

    e.error

    Answer:c

    Reason: The boolean() function for [](empty array) returns true

  5. Identify the output of the below program

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

    a.NaN

    b.error

    c.false

    d.true

    e.error

    Answer: c

    Reason: The boolean() function for NaN returns false

  6. Identify the output of the below program

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

    a.0

    b.false

    c.true

    d.error

    e.undefined

    Answer: b

    Reason: The boolean() function for 0 returns false

  7. Identify the output of the below program

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

    a.error

    b.undefined

    c.null

    d.true

    e.false

    Answer: e

    Reason: The boolean() function for an empty string is false

  8. Identify the output of the below program

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

    a.error

    b.undefined

    c.JS

    d.true

    e.false

    Answer: d

    Reason: The boolean() function for a string is true

  9. Identify the output of the below program

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

    a.error

    b.undefined

    c.null

    d.true

    e.false

    Answer: d

    Reason: The boolean() function for an object string is true

  10. Identify the output of the below program

    let v = Boolean(-0);
    console.log(v);
    

    a.error

    b.undefined

    c.null

    d.true

    e.false

    Answer: e

    Reason: The boolean() function for -0 is false

Did you find this article valuable?

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