百度智能小程序 運(yùn)算符

2020-09-05 15:17 更新

基本運(yùn)算符

代碼示例

var a = 1, b = 2;

// 加法運(yùn)算
console.log(3 === a + b);
// 減法運(yùn)算
console.log(-1 === a - b);
// 乘法運(yùn)算
console.log(2 === a * b);
// 除法運(yùn)算
console.log(0.5 === a / b);
// 取余運(yùn)算
console.log(1 === a % b);

加法運(yùn)算(+)也可以用作字符串的拼接。

var a = 'swan' , b = ' sjs';

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

一元運(yùn)算符

代碼示例

var a = 10, b = 20;

// 自增運(yùn)算
console.log(10 === a++);
console.log(12 === ++a);
// 自減運(yùn)算
console.log(12 === a--);
console.log(10 === --a);
// 正值運(yùn)算
console.log(10 === +a);
// 負(fù)值運(yùn)算
console.log(0-10 === -a);
// 否運(yùn)算
console.log(-11 === ~a);
// 取反運(yùn)算
console.log(false === !a);
// delete 運(yùn)算
console.log(true === delete a.fake);
// void 運(yùn)算
console.log(undefined === void a);
// typeof 運(yùn)算
console.log("number" === typeof a);

位運(yùn)算符

代碼示例

var a = 10, b = 20;

// 左移運(yùn)算
console.log(80 === (a << 3));
// 無(wú)符號(hào)右移運(yùn)算
console.log(2 === (a >> 2));
// 帶符號(hào)右移運(yùn)算
console.log(2 === (a >>> 2));
// 與運(yùn)算
console.log(2 === (a & 3));
// 異或運(yùn)算
console.log(9 === (a ^ 3));
// 或運(yùn)算
console.log(11 === (a | 3));

比較運(yùn)算符

代碼示例

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

等值運(yùn)算符

代碼示例

var a = 10, b = 20;

// 等號(hào)
console.log(false === (a == b));
// 非等號(hào)
console.log(true === (a != b));
// 全等號(hào)
console.log(false === (a === b));
// 非全等號(hào)
console.log(true === (a !== b));

賦值運(yùn)算符

代碼示例

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

二元邏輯運(yùn)算符

代碼示例

var a = 10, b = 20;

// 邏輯與
console.log(20 === (a && b));
// 邏輯或
console.log(10 === (a || b));

其他運(yùn)算符

代碼示例

var a = 10, b = 20;

//條件運(yùn)算符
console.log(20 === (a >= 10 ? a + 10 : b + 10));
//逗號(hào)運(yùn)算符
console.log(20 === (a, b));

運(yùn)算符優(yōu)先級(jí)

優(yōu)先級(jí)運(yùn)算符說(shuō)明結(jié)合性
20( ... )括號(hào)n/a
19... . ...成員訪問(wèn)從左到右
... [ ... ]成員訪問(wèn)從左到右
... ( ... )函數(shù)調(diào)用從左到右
17... ++后置遞增n/a
... --后置遞減n/a
16! ...邏輯非從右到左
~ ...按位非從右到左
+ ...一元加法從右到左
- ...一元減法從右到左
++ ...前置遞增從右到左
-- ...前置遞減從右到左
typeof ...typeof從右到左
void ...void從右到左
delete ...delete從右到左
14... * ...乘法從左到右
... / ...除法從左到右
... % ...取模從左到右
13... + ...加法從左到右
... - ...減法從左到右
12... << ...按位左移從左到右
... >> ...按位右移從左到右
... >>> ...無(wú)符號(hào)右移從左到右
11... < ...小于從左到右
... <= ...小于等于從左到右
... > ...大于從左到右
... >= ...大于等于從左到右
10... == ...等號(hào)從左到右
... != ...非等號(hào)從左到右
... === ...全等號(hào)從左到右
... !== ...非全等號(hào)從左到右
9... & ...按位與從左到右
8... ^ ...按位異或從左到右
7... | ...按位或從左到右
6... && ...邏輯與從左到右
5... || ...邏輯或從左到右
4... ? ... : ...條件運(yùn)算符從右到左
3... = ...賦值從右到左
... += ...賦值從右到左
... -= ...賦值從右到左
... *= ...賦值從右到左
... /= ...賦值從右到左
... %= ...賦值從右到左
... <<= ...賦值從右到左
... >>= ...賦值從右到左
... >>>= ...賦值從右到左
... &= ...賦值從右到左
... ^= ...賦值從右到左
... |= ...賦值從右到左
0... , ...逗號(hào)從左到右


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)