首页 > 解决方案 > 为什么“真||未定义?未定义:假;” 返回“未定义”?

问题描述

我在 chrome 控制台中写了这个表达式:

true || undefined ? undefined : false;

它返回:

undefined

怎么来的? 在此处输入图像描述

标签: javascript

解决方案


条件 ( true || undefined) 是true,因此,三元运算符undefined作为结果:

const condition = true || undefined;
console.log("condition:", condition);

console.log("result:", condition ? undefined : false);

如果您的目标是除以||

const result = true || (undefined ? undefined : false);

console.log("result:", result);


推荐阅读