gpt4 book ai didi

javascript - 为什么下面的是真的 : "Dog" === ("Cat" && "Dog")

转载 作者:数据小太阳 更新时间:2023-10-29 05:00:11 25 4
gpt4 key购买 nike

为什么 && 运算符返回最后一个值(如果语句为真)?

("Dog" == ("Cat" || "Dog")) // false
("Dog" == (false || "Dog")) // true
("Dog" == ("Cat" && "Dog")) // true
("Cat" && true) // true
(false && "Dog") // false
("Cat" && "Dog") // Dog
("Cat" && "Dog" && true) // true
(false && "Dog" && true) // false
("Cat" && "Dog" || false); // Dog

Fiddle

最佳答案

Logical Operators - && (MDN)

Returns expr1 if it can be converted to false; otherwise, returns expr2. Thus, when used with Boolean values, && returns true if both operands are true; otherwise, returns false.

对于您的表达式 "Cat"&& "Dog" ,第一个表达式 "Cat" 无法转换为 false 或 bool 值,因此它返回 “狗”

关于javascript - 为什么下面的是真的 : "Dog" === ("Cat" && "Dog"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21830556/

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