大于运算符在 javascript 中的 console.log 中给出了错误的响应

Greater than operator given wrong response inside console.log in javascript

我已经在 console.log() 中尝试了条件运算符。小于 < returns 正确响应但大于 > returns 错误响应。为什么?

    console.log(5<6<7);
    //Output: true

    console.log(7>6>5);
    //output: false

< 运算符接受两个操作数并产生布尔结果。您的代码有效:

let temp = 7 > 6;
console.log(temp);      // true
let result = temp > 5;
console.log(result);    // false

之所以temp > 5false是因为true > 5true强制转换为一个数字(1),而1 > 5是假的.

Insead,如果您需要逻辑 AND 条件,请使用 &&

console.log(7 > 6 && 6 > 5); // true

小于和大于运算符从左到右求值(参见Operator precedence):

5 < 6 < 7 = (5 < 6) < 7 = true < 7 = 1 < 7 = true

7 > 6 > 5 = (7 > 6) > 5 = true > 5 = 1 > 5 = false

布尔值 (false/true) 在这里被转换为整数 (0/1)。 ("If one of the operands is Boolean, the Boolean operand is converted to 1 if it is true and +0 if it is false." (source: MDN))