为什么在条件语句中不将 null 转换为布尔值?
why null is not converted to a boolean value in a conditional statement?
有如下测试用例:(find fiddle here)
var a = new Date();
var b = null;
var c = {test: "test"};
if(a)
console.log(a); //--- prints the current date
if(b)
console.log('null'); //--- never reached
if(c)
console.log('test'); //--- prints 'test'
console.log(a && b); //--- prints null
知道
console.log(typeof null); //--- prints "object"
console.log(typeof c); //--- prints "object"
我期待
的结果
console.log(a && b);
为 false 而不是示例中所示的 null。
有什么提示吗?
来自the MDN:
expr1 && expr2
: Returns expr1 if it can be converted to false; otherwise, returns expr2
new Date
无法转换为false
(不是falsy),所以返回b
。
来自 MDN:
Logical AND (&&) expr1 && expr2 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.
I expect the result of
console.log(a && b);
to be false
and not null
as it shown in the example.
在许多带有 &&
和 ||
运算符(或 AND
和 OR
)的语言中,结果始终是布尔值,是的。* 但是 JavaScript 的 &&
和 ||
比那个更有用:它们的结果是它们的左手操作数的值或它们的右手操作数的值, 不是 强制转换为布尔值。
&&
的工作原理如下:
计算左边的操作数。
如果值为 falsey(当我们将其设为布尔值时强制为 false
),return 值(不是强制值)
如果来自 #2 的值是 truthy,计算并 return 右边的值(未强制)
falsey 值为 null
、undefined
、0
、""
、NaN
和当然,false
。其他的都是 truthy.
例如
console.log(0 && 'hi'); // 0
...显示 0
因为 0
是错误的。请注意,结果是 0
,而不是 false
。同样,这里的结果:
console.log(1 && 'hello'); // hello
...是 'hello'
因为右侧根本没有被 &&
.
强制
||
的工作方式类似(我称之为 curiously-powerful OR operator):它计算左侧,如果是 truthy,则将其作为结果;否则它计算右侧并取 that 作为结果。
唯一一次从 &&
或 ||
得到 true
或 false
的情况是所选操作数 已经 一个布尔值。
* 当然,许多(但不是全部)这些语言(Java、C#)也要求 &&
和 ||
的操作数已经是布尔值。 C 是一个例外,它进行了一些强制转换,但仍然将操作数的结果作为布尔值(或者如果你回溯得足够远,则为 1 或 0 的 int)。
有如下测试用例:(find fiddle here)
var a = new Date();
var b = null;
var c = {test: "test"};
if(a)
console.log(a); //--- prints the current date
if(b)
console.log('null'); //--- never reached
if(c)
console.log('test'); //--- prints 'test'
console.log(a && b); //--- prints null
知道
console.log(typeof null); //--- prints "object"
console.log(typeof c); //--- prints "object"
我期待
的结果console.log(a && b);
为 false 而不是示例中所示的 null。
有什么提示吗?
来自the MDN:
expr1 && expr2
: Returns expr1 if it can be converted to false; otherwise, returns expr2
new Date
无法转换为false
(不是falsy),所以返回b
。
来自 MDN:
Logical AND (&&) expr1 && expr2 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.
I expect the result of
console.log(a && b);
to be
false
and notnull
as it shown in the example.
在许多带有 &&
和 ||
运算符(或 AND
和 OR
)的语言中,结果始终是布尔值,是的。* 但是 JavaScript 的 &&
和 ||
比那个更有用:它们的结果是它们的左手操作数的值或它们的右手操作数的值, 不是 强制转换为布尔值。
&&
的工作原理如下:
计算左边的操作数。
如果值为 falsey(当我们将其设为布尔值时强制为
false
),return 值(不是强制值)如果来自 #2 的值是 truthy,计算并 return 右边的值(未强制)
falsey 值为 null
、undefined
、0
、""
、NaN
和当然,false
。其他的都是 truthy.
例如
console.log(0 && 'hi'); // 0
...显示 0
因为 0
是错误的。请注意,结果是 0
,而不是 false
。同样,这里的结果:
console.log(1 && 'hello'); // hello
...是 'hello'
因为右侧根本没有被 &&
.
||
的工作方式类似(我称之为 curiously-powerful OR operator):它计算左侧,如果是 truthy,则将其作为结果;否则它计算右侧并取 that 作为结果。
唯一一次从 &&
或 ||
得到 true
或 false
的情况是所选操作数 已经 一个布尔值。
* 当然,许多(但不是全部)这些语言(Java、C#)也要求 &&
和 ||
的操作数已经是布尔值。 C 是一个例外,它进行了一些强制转换,但仍然将操作数的结果作为布尔值(或者如果你回溯得足够远,则为 1 或 0 的 int)。