gpt4 book ai didi

javascript - 为什么 programm (massif) 会是真的?

转载 作者:行者123 更新时间:2023-11-29 15:08:33 24 4
gpt4 key购买 nike

所以,我知道:null""undefined,0, NaN 将返回 false并且 JS 中的所有其他值都将为 true,但为什么:

console.log([] - 1)          // -1 (it`s mean that [] = 0 (value of false?)
console.log([] - []) // 0 (wtf?),[] = 1?
if([]) console.log('true') // true
console.log(null - 1) // -1
if(null) console.log('true') // (false, no output)

谁能给我解释一下这是怎么回事吗?

最佳答案

这是一个算术表达式。根据ECMAScript Language Specification,算术表达式中不同类型的转换如下:

enter image description here

在表达式中,数组 ([]) 被视为对象 并相应地转换为。

完整引用 here .

关于javascript - 为什么 programm (massif) 会是真的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57223968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com