In the Google JavaScript style guide, it says not to use wrapper objects for primitive types. It says it's "dangerous" to do so. To prove its point, it uses the example:
var x = new Boolean(false);
if (x) {
alert('hi'); // Shows 'hi'.
}
OK, I give up. Why is the if code being executed here?
if(x)
will run if x
is truthy.
x
is truthy if it's not falsey.
x is falsey if x is null
, undefined
, 0
, ""
, false
So since new Boolean(false)
is an Object
and an Object
is truthy, the block runs
Because every variable that is typeof
Object
is truthy and wrappers are objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With