简体   繁体   English

JavaScript运算符的优先级和关联性

[英]JavaScript operator precedence and associativity

In JavaScript 在JavaScript中

var x = (5 >= 9 >= 0);
console.log(x);
// returns true

What is happening in that statement and what is the reason for that output? 该语句中发生了什么,输出的原因是什么?

The Operator precedence and Associativity chart explains what is happening. 运算符优先级和关联性图表说明了正在发生的事情。 The expression is evaluated in this order: 表达式按以下顺序求值:

/* 1 */ x = (5 >= 9) >= 0;
/* 2 */ x = false >= 0;
/* 3 */ x = true;    

Explanation: 说明:

  1. >= is left associative the operator between 5 and 9 is evaluated first. >=左键关联,首先评估5到9之间的运算符。
  2. >= uses abstract comparison which converts boolean to number ie false becomes 0 ( ref ). >=使用抽象比较将布尔值转换为数字,即false变为0( ref )。
  3. The end result is true . 最终结果是true

The true statement, is understood as different from zero ... Consequently, false is equals zero ... true陈述应理解为不同于零 ...因此, false 等于零 ...

The statement: 该声明:

(5>=9>=0)

Turns into 变成

(false >= 0)

Since 5 is lower than 9. Then, if false is equal zero the result is true 由于5小于9。因此,如果false等于零,则结果为true

EDIT 编辑

As explained here ( All falsey values in JavaScript ), zero is among the falsy values in javascript... 如此处所述( JavaScript中的所有虚假值 ), JavaScript中的虚假值中有零...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM