为什么这个和/或场景评估为假?

时间:2018-03-06 22:25:30

标签: javascript operators

为什么这会返回false

var flavor = "chocolate";
console.log(flavor == ("vanilla" || "chocolate")); 

如果我改为输入:

var flavor = "chocolate";
console.log(flavor == ("vanilla" && "chocolate"));

它返回true。这对我来说没有意义,因为从逻辑上讲,味道不能与巧克力和香草相提并论。有人能帮助我理解我应该如何思考这个问题吗?

1 个答案:

答案 0 :(得分:0)

Auth.js返回 false ,因为(flavor == ("vanilla" || "chocolate"))返回("vanilla" || "chocolate"),因此您实际上正在比较"vanilla",它返回

"chocolate" == "vanilla"返回 true ,因为(flavor == ("vanilla" && "chocolate"))返回("vanilla" && "chocolate"),因此"chocolate"显然会返回 true

我认为你真正想要的是"chocolate" == "chocolate"(flavor == "vanilla" || flavor == "chocolate")。尝试一下:)