[英]what the operator “ &&” do in detail in the following snippet?
I want to know why the property's value is not "true" in hash object, I assign the property into "true" in the statement " hash.jason = true ".我想知道为什么hash object中的属性值不是“true”,我在语句“hash.jason = true”中将属性赋值为“true”
var array=[]
var array=[]
hash.jason = true && array.push('jason')
hash.tom = true && array.push('tom')
hash.lucy = true && array.push('lucy')
the output is:
array
(3) ["jason", "tom", "lucy"]
hash
{jason: 1, tom: 2, lucy: 3}
&& is logical AND. && 是逻辑与。 Next,
array.push
returns length of updated array, so you assign it to hash.接下来,
array.push
返回更新数组的长度,因此您将其分配给 hash。 But, the same result you'll also get without true &&
但是,如果没有
true &&
,你也会得到同样的结果
const array = []; const hash = {}; // array.push itself returns length of // updated array, so here you // assign this length to hash hash.jason = true && array.push('jason'); // But will not push to array if false hash.tom = false && array.push('tom'); // Without 'true &&' will do the same hash.lucy = array.push('lucy'); // Log console.log(array) console.log(hash)
Second part of your question - why it's assigning not true
but number
, see little code below... The logical AND expression is evaluated left to right, it is tested for possible "short-circuit" evaluation using the following rule: (some falsy expression) && expr
你问题的第二部分 - 为什么它分配不是
true
而是number
,请参见下面的小代码......逻辑 AND 表达式从左到右进行评估,使用以下规则测试可能的“短路”评估:( (some falsy expression) && expr
console.log(true && 13); console.log(false && 99);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.