Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.prototype.every() boolean values

Tags:

javascript

I'm confused by this .every() behavior.

let a = [true, true, true]
a.every(Boolean) //  returns true

let b = [true, false, true]
b.every(Boolean) // returns false

typeof false // returns 'boolean'

I can not understand why b.every(Boolean) returns false . What do i miss ?

like image 647
Noob Avatar asked Jun 14 '26 06:06

Noob


1 Answers

From MDN:

The every() method tests whether all elements in the array pass the test implemented by the provided function.

The Boolean callback that you use converts the variable passed to it to a boolean, so Boolean(false) will return false, which makes b.every(Boolean) return false as well.

like image 74
ngranko Avatar answered Jun 17 '26 09:06

ngranko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!