[摘要]基本运算符示例代码:var a = 10, b = 20;// 加法运算console.log(30 === a + b);// 减法运算console.log(-10 === a - b);// ...
基本运算符
示例代码:
var a = 10, b = 20;// 加法运算console.log(30 === a + b);// 减法运算console.log(-10 === a - b);// 乘法运算console.log(200 === a * b);// 除法运算console.log(0.5 === a / b);// 取余运算console.log(10 === a % b);
var a = '.w' , b = 'xs';// 字符串拼接console.log('.wxs' === a + b);一元运算符
示例代码:
var a = 10, b = 20;// 自增运算console.log(10 === a++);console.log(12 === ++a);// 自减运算console.log(12 === a--);console.log(10 === --a);// 正值运算console.log(10 === +a);// 负值运算console.log(0-10 === -a);// 否运算console.log(-11 === ~a);// 取反运算console.log(false === !a);// delete 运算console.log(true === delete a.fake);// void 运算console.log(undefined === void a);// typeof 运算console.log("number" === typeof a);位运算符
示例代码:
var a = 10, b = 20;// 左移运算console.log(80 === (a << 3));// 无符号右移运算console.log(2 === (a >> 2));// 带符号右移运算console.log(2 === (a >>> 2));// 与运算console.log(2 === (a & 3));// 异或运算console.log(9 === (a ^ 3));// 或运算console.log(11 === (a 3));比较运算符
示例代码:
var a = 10, b = 20;// 小于console.log(true === (a < b));// 大于console.log(false === (a > b));// 小于等于console.log(true === (a <= b));// 大于等于console.log(false === (a >= b));等值运算符
示例代码:
var a = 10, b = 20;// 等号console.log(false === (a == b));// 非等号console.log(true === (a != b));// 全等号console.log(false === (a === b));// 非全等号console.log(true === (a !== b));赋值运算符
示例代码:
var a = 10;
a = 10; a *= 10;console.log(100 === a);
a = 10; a /= 5;console.log(2 === a);
a = 10; a %= 7;console.log(3 === a);
a = 10; a += 5;console.log(15 === a);
a = 10; a -= 11;console.log(-1 === a);
a = 10; a <<= 10;console.log(10240 === a);
a = 10; a >>= 2;console.log(2 === a);
a = 10; a >>>= 2;console.log(2 === a);
a = 10; a &= 3;console.log(2 === a);
a = 10; a ^= 3;console.log(9 === a);
a = 10; a = 3;console.log(11 === a);二元逻辑运算符
示例代码:
var a = 10, b = 20;// 逻辑与console.log(20 === (a && b));// 逻辑或console.log(10 === (a b));其他运算符
示例代码:
var a = 10, b = 20;//条件运算符console.log(20 === (a >= 10 ? a + 10 : b + 10));//逗号运算符console.log(20 === (a, b));运算符优先级