Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program operator


May 18, 2021 WeChat Mini Program Development Document


Table of contents


The basic operator

Example code:

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);
  • Addition operations can also be used as stitching strings.
var a = '.w' , b = 'xs';

// 字符串拼接
console.log('.wxs' === a + b);

A dollar operator

Example code:

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);

Bit operator

Example code:

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));

Comparison operator

Example code:

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));

Equivalent operator

Example code:

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));

The assignment operator

Example code:

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);

Binary logic operator

Example code:

var a = 10, b = 20;

// 逻辑与
console.log(20 === (a && b));
// 逻辑或
console.log(10 === (a || b));

Other operators

Example code:

var a = 10, b = 20;

//条件运算符
console.log(20 === (a >= 10 ? a + 10 : b + 10));
//逗号运算符
console.log(20 === (a, b));

Operator priority

Priority Operator Description Binding
20 ( ... ) Brackets n/a
19 ... . ... Member access Left to right
... [ ... ] Member access Left to right
... ( ... ) The function is called Left to right
17 ... ++ Post-increment n/a
... -- The post is decreasing n/a
16 ! ... Logic is not Right to left
~ ... By bit non Right to left
+ ... One dollar plus method Right to left
- ... One-dollar subtract Right to left
++ ... Pre-increment Right to left
-- ... The front is decreasing Right to left
typeof ... typeof Right to left
void ... void Right to left
delete ... delete Right to left
14 ... * ... Multiplication Left to right
... / ... Division Left to right
... % ... Molding Left to right
13 ... + ... Addition Left to right
... - ... Subtraction Left to right
12 ... << ... Move left by bit Left to right
... >> ... Move right by bit Left to right
... >>> ... Unsigned right shift Left to right
11 ... < ... Less than Left to right
... <= ... Less than or equal to Left to right
... > ... Greater than Left to right
... >= ... Is greater than or equal to Left to right
10 ... == ... Equals Left to right
... != ... Non-equal sign Left to right
... === ... Full equivalent Left to right
... !== ... Non-full equal sign Left to right
9 ... & ... By bit with Left to right
8 ... ^ ... By bit or Left to right
7 ... ... By bit or Left to right
6 ... && ... Logic and Left to right
5 ... || ... Logic or Left to right
4 ... ? ... : ... Conditional operator Right to left
3 ... = ... Assignment Right to left
... += ... Assignment Right to left
... -= ... Assignment Right to left
... *= ... Assignment Right to left
... /= ... Assignment Right to left
... %= ... Assignment Right to left
... <<= ... Assignment Right to left
... >>= ... Assignment Right to left
... >>>= ... Assignment Right to left
... &= ... Assignment Right to left
... ^= ... Assignment Right to left
... |= ... Assignment Right to left
0 ... , ... Comma Left to right