Namaste, In this blog I will discuss 8 code scenarios on Boolean() Object and Boolean() function.
Identify the output of the below program
let ob = Boolean(); console.log(ob);
a.true
b.false
c.null
d.undefined
e.error
Answer: b
Reason: The boolean() function returns false
Identify the output of the below program
let ob = new Boolean(); console.log(ob);
a.Boolean(false)
b.false
c.true
d.undefined
e.error
Answer: a
Reason: The boolean() object for empty value returns Boolean(false)
Identify the output of the below program
let ob = new Boolean(false); console.log(ob);
a.true
b.false
c.undefined
d.Boolean(false)
e.error
Answer: d
Reason: The boolean() object returns Boolean(false)
Identify the output of the below program
let ob = new Boolean(false); if (ob) console.log("true"); else console.log("false");
a.true
b.false
c.error
d.undefined
e.Boolean(false)
Answer: a
Reason: The ob is an object hence "true" is printed
Identify the output of the below program
let ob = new Boolean("JS"); console.log(ob);
a.Boolean(false)
b.false
c.true
d.Boolean(true)
e.error
Answer: d
Reason: The new boolean("JS") object returns Boolean(true)
Identify the output of the below program
let ob = Boolean(false); if (ob) console.log("true"); else console.log("false");
a.false
b.true
c.undefined
d.Boolean(false)
e.true
Answer: a
Reason: ob is a boolean primitive with value false, not an object
Identify the output of the below program
let ob = Boolean(false); console.log(typeof ob);
a.object
b.boolean
c.string
d.number
e.error
Answer: b
Reason: The Boolean() function returns a boolean value
Identify the output of the below program
let ob = new Boolean(false); console.log(typeof ob);
a.object
b.boolean
c.string
d.number
e.error
Answer: a
Reason: When new is used an object is created.