简体   繁体   English

为什么这个 function 返回 boolean 值?

[英]Why does this function return boolean values?

I was wondering why this function returns true and false values?我想知道为什么这个 function 返回真值和假值? I wasn't expecting numbers necessarily, but am confused - is it something to do with how the forEach method works?我并不一定期望数字,但我很困惑 - 这与 forEach 方法的工作原理有关吗?

 function canDrive(...years){ years.forEach(cur => console.log((2020 - cur) >= 21)); } canDrive(1990, 1999, 1965)

It is "returning" boolean values because there is a comparison statement inside of the console.log:它正在“返回” boolean 值,因为在 console.log 中有一个比较语句:

console.log((2020 - cur) >= 21)

This is converting it to a boolean.这是将其转换为 boolean。 (it is actually logging, not returning as @Bergi mentioned in the comments) (它实际上是在记录,而不是像评论中提到的@Bergi那样返回)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM