js中不常見的運算符與操作符總結(jié)
javaScript常用運算符和操作符總結(jié)
|
類別 |
操作符 |
| 算術(shù)操作符 | +、 –、 *、 /、 %(取模) |
| 字符串操作符 | + 字符串連接 +=字符串連接復(fù)合 |
| 布爾操作符 | !、 &&、 || |
| 一元操作符 | ++ 、 -- 、 +(一元加)、 -(一元減) |
| 關(guān)系比較操作符 | < 、 <= 、 > 、>=、 != 、 == 、 === 、 !== |
| 按位操作符 | ~ 按位非 &按位與 | 按位或 ^按位異或 <<左移 >>有符號右移 >>>無符號右移 |
| 賦值操作符 | = 、 復(fù)合賦值(+=、-=、*=、%=) 復(fù)合按位賦值(~=、&=、|=、^=、<<=、>>=、>>>=) |
| 對象操作符 | .屬性訪問、[]屬性或數(shù)組訪問、 new調(diào)用構(gòu)造函數(shù)常見對象、delete變量屬性刪除、void(返回undefined)、in判斷屬性、instanceof原型判斷 |
| 其它操作符 | ?: 條件操作符、,逗號操作符、()分組操作、typeof類型操作符 |
js中不常見的運算符與操作符
空值合并運算符: ??
當(dāng)左側(cè)的操作數(shù)為 null 或者 undefined 時,返回其右側(cè)操作數(shù),否則返回左側(cè)操作數(shù)。
null ?? 'huli' // huli
undefined ?? 'huli' // undefined
'' ?? 'huli' // ''
[] ?? 'huli' // []
({}) ?? 'huli' // {}
NaN ?? 'huli' // NaN
false ?? 'huli' // false
0 ?? 'huli' // 0
邏輯空賦值: ??=
邏輯空賦值運算符 (x ??= y) 僅在 x 是 nullish (null 或 undefined) 時對其賦值。
const a = { duration: 50 };
a.duration ??= 10;
console.log(a.duration);
// expected output: 50
a.speed ??= 25;
console.log(a.speed);
// expected output: 25
邏輯或: ||
存在真則為真, , 以前面的為準(zhǔn)
const a = 3; const b = -2; console.log(a > 0 || b > 0); // true
可以是false的值
null
undefined
NaN
"" 與 ''
0
false
邏輯或賦值: ||=
有則返回,沒有則賦值
const a = { duration: 50, title: '' };
a.duration ||= 10;
console.log(a.duration);
// expected output: 50
a.title ||= 'title is empty.';
console.log(a.title);
// expected output: "title is empty"
邏輯與: &&
都存在則為真, 以后面的為準(zhǔn)
const a = 3; const b = -2; console.log(a > 0 && b > 0); // expected output: false
邏輯與賦值: &&=
存在則賦值
let a = 1; let b = 0; a &&= 2; console.log(a); // expected output: 2 b &&= 2; console.log(b); // expected output: 0
可選鏈操作符: ?.
可選鏈操作符( ?. )允許讀取位于連接對象鏈深處的屬性的值,而不必明確驗證鏈中的每個引用是否有效。?. 操作符的功能類似于 . 鏈?zhǔn)讲僮鞣煌幵谟?,在引用為?nullish ) (null 或者 undefined) 的情況下不會引起錯誤,該表達(dá)式短路返回值是 undefined。與函數(shù)調(diào)用一起使用時,如果給定的函數(shù)不存在,則返回 undefined。
const adventurer = {
name: 'Alice',
cat: {
name: 'Dinah'
}
};
const dogName = adventurer.dog?.name;
console.log(dogName);
// expected output: undefined
console.log(adventurer.someNonExistentMethod?.());
// expected output: undefined
總結(jié)
到此這篇關(guān)于js中不常見的運算符與操作符的文章就介紹到這了,更多相關(guān)js運算符與操作符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
對javascript的一點點認(rèn)識總結(jié)《javascript高級程序設(shè)計》讀書筆記
Javascript專為與網(wǎng)頁交互而設(shè)計的腳本語言,由下列三個部門構(gòu)造2011-11-11
讓innerText在firefox火狐和IE瀏覽器都能用的寫法
下面的代碼主要是用來解決firefox瀏覽器不支持innerText的問題,需要的朋友可以參考下。2011-05-05

