Our code :-
Line 1: console.log(Boolean({}))
What's Happening?
{}
is an empty object.
In JavaScript:
- All objects are truthy, even empty ones.
Boolean({})
→true
Output: true
Line 2: console.log(Boolean([]))
What's Happening?
[]
is an empty array, which is also an object.
Again:
- All objects are truthy, including arrays (even empty ones).
Boolean([])
→true
Output: true
Line 3: console.log(Boolean(""))
What's Happening?
""
is an empty string, which is one of the falsy primitive values in JavaScript.
List of falsy values includes:
false
,0
,""
,null
,undefined
,NaN
So:
Boolean("")
→false
Output: false
Line 4: Boolean(new Boolean(false))
This is super tricky and often misunderstood.
What’s going on?
new Boolean(false)
creates a Boolean object, not a primitive.- It wraps the value
false
, but it's still an object.
Remember:
All objects are truthy, even if they represent a falsy value.
So:
💡 Note: This line does not log anything because it’s not inside console.log()
, but the value is true
.
Key points :-
- Primitive
false
is falsy, butnew Boolean(false)
is an object, which is truthy. - Avoid
new Boolean()
,new String()
, andnew Number()
— they create wrapper objects that can lead to unexpected behavior. - Use
Boolean(value)
or!!value
to test truthiness reliably.
✔ Final Answer:
true // Line 2
false // Line 3
// Line 4 does nothing visible, but returns true
B) true true false true
NOTE :- when we run our code on browser console, we got above output where below code print true.
Boolean(new Boolean(false));This question appeared in the Publicis Sapient 2nd round of interview, testing the candidate’s knowledge of type coercion in JavaScript.