Introduction
Namaste, In this blog I will discuss 10 code scenarios on the Boolean() function.
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
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
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
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
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
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
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
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
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
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