I would like to write array.some(Boolean)
as a way to test if any elements in array
are truthy.
Are there any issues with using the Boolean
object in this way? Does it have the same behavior across modern browsers? Is it always equivalent to array.some(function(elem) { return !!elem; })
?
Are there any issues with using the
Boolean
object in this way?
No. Unlike parseInt
for example (which would be problematic), Boolean
only expects a single argument. So there shouldn't be any issues with passing the other callback arguments to it (index and the array), it will simply ignore them.
Does it have the same behavior across modern browsers?
I do hope so. If you can't trust the Boolean
function, what else is left?
Is it always equivalent to
array.some(function(elem) { return !!elem; })
?
Yes. From the spec:
When
Boolean
is called as a function rather than as a constructor, it performs a type conversion.
And that's essentially what !!
does as well.
Relevant references from the spec:
Boolean
function!
operatorToBoolean
functionIf 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