返回首页 前端开发笔记本

表达式与运算符

表达式

表达式为 JavaScript 的短语可执行并生成值。

1.7 // 字面量
"1.7"
var a = 1;
var b = '2';
var c = (1.7 + a) * '3' - b

运算符

  • 算数运算符 (+ - * / %
  • 关系运算符 (> < == != >= <= === !==)
  • 逻辑运算符 (! && ||
  • 位运算符 (& | ^ ~ << >>
  • 负值运算符 (=
  • 条件运算符 (?:
  • 逗号运算符 (,
  • 对象运算符 (new delete . [] instanceof

=== 全等符号

全等运算符用于盘对左右两边的对象或值是否类型相同且值相等。

伪代码拆解

function totalEqual(a, b) {
  if (a 和 b 类型相同) {
    if (a 和 b 是引用类型) {
      if (a 和 b 是同一引用)
        return true;
      else
        return false;
    } else { // 值类型
      if (a 和 b 值相等)
        return true;
      else
        return false;
    }
  } else {
    return false;
  }
}

例子

var a = "123";
var b = "123";
var c = "4";
var aObj = new String("123");
var bObj = new String("123");
var cObj = aObj;

a === aObj      // false
aObj === bObj   // false
aObj === cObj   // true
a === b         // true
a === c         // false

==

== 用于判断操作符两边的对象或值是否相等。

伪代码拆解

function equal(a, b) {
  if (a 和 b 类型相同) {
    return a === b;
  } else { // 类型不同
    return Number(a) === Number(b); // 优先转换数值类型
  }
}

例子

"99" == 99; // true
new String("99") == 99; // true
true == 1; // true
false == 0; // true
'\n\n\n' == // true
例外规则
  • null == undefined 结果为真 true
  • 在有 null/undefined 参与的 == 运算是不进行隐式转换。
0 == null; // false
null == false; // false
"undefined" == undefined; // false

! 取反

!x 用于表达 x 表达式的运行结果转换成布尔值(Boolean)之后取反的结果。!!x 则表示取 x 表达式的运行结果的布尔值。

var obj = {};
var a = !obj // false;
var a = !!obj // true;

&& 逻辑与

x && y 如果 x 表达式的运行交过转换成 Boolean 值为 false 则不运行表达式 y 而直接返回 x 表达式的运行结果。相反,如果 x 表达式的运行交过转换成 Boolean 值为 true 则运行表达式 y 并返回 y 表达式的运行结果。

伪代码拆解

var ret = null;
if (!!(ret = x)) {
  return y;
} else {
  return ret;
}

例子

var a = 0 && (function(){return 1 + 1;})(); // 0
var b = 1 && (function(){return 1 + 1;})(); // 2

|| 逻辑或

x || y 如果 x 表达式的运行结果转换为 Boolean 值为 true,则不运行 表达式 y 而直接返回表达式 x 的运算结果。(与 && 方式相反)

伪代码拆解

var ret = null;
if (!!(ret = x)) {
  return ret;
} else {
  return y;
}

例子

var a = 0 || (function(){return 1 + 1;})(); // 2
var b = 1 || (function(){return 1 + 1;})(); // 1

元算符优先级(Operator Precedence)

  • + - * / 高于 &&
  • * / 高于 + -
  • && 高于 ?:
  • () 内优先级高于之外

NOTE:和数学上的算术优先级类似,同级从左到右计算。如有疑问加上 () 既可解决优先级问题。

Precedence Operator type Associativity Individual operators
19 Grouping n/a ( … )
18 Member Access left-to-right … . …
Computed Member Access left-to-right … [ … ]
new (with argument list) n/a new … ( … )
17 Function Call left-to-right … ( … )
new (without argument list) right-to-left new …
16 Postfix Increment n/a … ++
Postfix Decrement n/a … --
15 Logical NOT right-to-left ! …
Bitwise NOT right-to-left ~ …
Unary Plus right-to-left + …
Unary Negation right-to-left - …
Prefix Increment right-to-left ++ …
Prefix Decrement right-to-left -- …
typeof right-to-left typeof …
void right-to-left void …
delete right-to-left delete …
14 Multiplication left-to-right … * …
Division left-to-right … / …
Remainder left-to-right … % …
13 Addition left-to-right … + …
Subtraction left-to-right … - …
12 Bitwise Left Shift left-to-right … << …
Bitwise Right Shift left-to-right … >> …
Bitwise Unsigned Right Shift left-to-right … >>> …
11 Less Than left-to-right … < …
Less Than Or Equal left-to-right … <= …
Greater Than left-to-right … > …
Greater Than Or Equal left-to-right … >= …
in left-to-right … in …
instanceof left-to-right … instanceof …
10 Equality left-to-right … == …
Inequality left-to-right … != …
Strict Equality left-to-right … === …
Strict Inequality left-to-right … !== …
9 Bitwise AND left-to-right … & …
8 Bitwise XOR left-to-right … ^ …
7 Bitwise OR left-to-right … | …
6 Logical AND left-to-right … && …
5 Logical OR left-to-right … || …
4 Conditional right-to-left … ? … : …
3 Assignment right-to-left … = …
… += …
… -= …
… *= …
… /= …
… %= …
… <<= …
… >>= …
… >>>= …
… &= …
… ^= …
… |= …
2 yield right-to-left yield …
1 Spread n/a ... …
0 Comma / Sequence left-to-right … , …
上一篇: 变量作用域 下一篇: 语句