大于操作员在javascript的console.log中给出错误响应 [英] Greater than operator given wrong response inside console.log in javascript
本文介绍了大于操作员在javascript的console.log中给出错误响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 console.log()
中尝试了条件运算符.小于<
会返回正确的响应,但是大于>
会返回错误的响应.为什么?
I have tried conditional operator inside console.log()
. Less than <
returns correct response but greater than >
returns wrong response. Why?
console.log(5<6<7);
//Output: true
console.log(7>6>5);
//output: false
推荐答案
<
运算符接受两个操作数,并产生布尔结果.您的代码有效:
The <
operator accepts two operands and yields a boolean result. Your code is effectively:
let temp = 7 > 6;
console.log(temp); // true
let result = temp > 5;
console.log(result); // false
原因 temp>5
是 false
是 true>5
将 true
强制转换为数字( 1
),然后将 1>5
是错误的.
The reason temp > 5
is false
is that true > 5
coerces true
to a number (1
), and 1 > 5
is false.
在Insead中,如果需要逻辑AND条件,请使用&
:
Insead, use &&
if you want a logical AND condition:
console.log(7 > 6 && 6 > 5); // true
这篇关于大于操作员在javascript的console.log中给出错误响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文