最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Array.prototype.every() boolean values - Stack Overflow

programmeradmin3浏览0评论

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 ?

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 ?

Share Improve this question asked Aug 20, 2019 at 0:27 NoobNoob 2,8074 gold badges23 silver badges43 bronze badges 2
  • 1 b.every(el => typeof el === "boolean") returns true. – user5734311 Commented Aug 20, 2019 at 0:38
  • @ChrisG yeah, i see now. Thanks – Noob Commented Aug 20, 2019 at 0:43
Add a ment  | 

1 Answer 1

Reset to default 15

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.

发布评论

评论列表(0)

  1. 暂无评论