ECMAScript 2020

Bersyon ng JavaScript

Ang lumang bersyon ng JS ay pinangalanan ng numero: ES5 (2009) at ES6 (2015).

mula 2016, ang bersyon ay pinangalanan ayon sa taon: ECMAScript 2016, 2017, 2018, 2019, ...

Bagong katangian sa ES2020:

Warning

Ang mga katangian na ito ay napakabagong lamang.

Ang mas lumang browser ay maaaring kailangan ng alternative code (Polyfill).

BigInt in JavaScript

Ang variable ng BigInt sa JavaScript ay ginagamit para sa pag-iimbak ng malalaking integer na hindi maaaring maisipagmataas ng pangkaraniwang JavaScript number.

Ang integer sa JavaScript ay maaring maayos lamang hanggang 15 na numero.

instance ng Integer

let x = 999999999999999;
let y = 9999999999999999; // napakalaki

亲自试一试

instance ng BigInt

let x = 9999999999999999;
let y = 9999999999999999n;

亲自试一试

Para sa paglikha ng BigInt, i-set sa n magdagdag sa huli ng integer o tumawag sa BigInt():

Example

let x = 1234567890123456789012345n;
let y = BigInt(1234567890123456789012345);

亲自试一试

Ang JavaScript type ng BigInt ay "bigint":

Example

let x = BigInt(999999999999999);
let type = typeof x;

亲自试一试

mula 2020 Setyembre 9, lahat ng modernong browser ay sumusuporta sa BigInt:

Chrome Edge Firefox Safari Opera
Chrome 67 Edge 79 Firefox 68 Safari 14 Opera 54
May 2018 January 2020 July 2019 2020 年 9 月 June 2018

JavaScript 字符串方法 matchAll()

Before ES2020, there was no string method available to search for all occurrences of a string in a string.

Example

const iterator = text.matchAll("Cats");

亲自试一试

If the parameter is a regular expression, you must set the global flag (g) or it will throw a TypeError.

Example

const iterator = text.matchAll(/Cats/g);

亲自试一试

If you want to perform a case-insensitive search, you must set the case-insensitive flag (i)

Example

const iterator = text.matchAll(/Cats/gi);

亲自试一试

Hint:ES2021 The string method replaceAll() was introduced.

Nullish Coalescing Operator (Nullish Coalescing Operator) (?? Operator)

If the first parameter is not a null value (null or undefined),then ?? Operator returns the first parameter.

otherwise return the second.

Example

let name = null;
let text = "missing";
let result = name ?? text;

亲自试一试

Since March 2020, all modern browsers have supported nullish operators:

Chrome Edge Firefox Safari Opera
Chrome 80 Edge 80 Firefox 72 Safari 13.1 Opera 67
February 2020 February 2020 January 2020 2020 年 3 月 2020 年 3 月

Optional Chaining Operator (Optional Chaining Operator) (?. Operator)

If the object is undefined or nullthenOptional Chaining OperatorReturns undefined(instead of throwing an error).

Example

const car = {type:"Fiat", model:"500", color:"white"};
let name = car?.name;

亲自试一试

Since March 2020, all modern browsers have supported ?.= 运算符:

Chrome Edge Firefox Safari Opera
Chrome 80 Edge 80 Firefox 74 Safari 13.1 Opera 67
February 2020 February 2020 2020 年 3 月 2020 年 3 月 2020 年 3 月

逻辑 AND 赋值运算符(&&= 运算符)

Logic AND Assignment Operatorused between two values.

If the first value is true,则分配第二个值。

Logic AND Assignment Example

let x = 100;
x &&= 5;

亲自试一试

自 2020 年 9 月以来,所有现代浏览器都支持 &&= 运算符:

Chrome Edge Firefox Safari Opera
Chrome 85 Edge 85 Firefox 79 Safari 14 Opera 71
2020 年 8 月 2020 年 8 月 2020 年 3 月 2020 年 9 月 2020 年 9 月

Logic OR Assignment Operator (||= Operator)

Logic OR Assignment Operatorused between two values.

If the first value is false,则分配第二个值。

Logic OR Assignment Example

let x = 10;
x ||= 5;

亲自试一试

自 2020 年 9 月以来,所有现代浏览器都支持 ||= 运算符:

Chrome Edge Firefox Safari Opera
Chrome 85 Edge 85 Firefox 79 Safari 14 Opera 71
2020 年 8 月 2020 年 8 月 2020 年 3 月 2020 年 9 月 2020 年 9 月

空值合并赋值运算符(??= 运算符)

空值合并赋值运算符(Nullish Coalescing Assignment Operator)用于两个值之间。

如果第一个值 undefined 或为 null,则分配第二个值。

空值合并赋值实例

let x = 10;
x ??= 5;

亲自试一试

自 2020 年 9 月以来,所有现代浏览器都支持 ??= 运算符:

Chrome Edge Firefox Safari Opera
Chrome 85 Edge 85 Firefox 79 Safari 14 Opera 71
2020 年 8 月 2020 年 8 月 2020 年 3 月 2020 年 9 月 2020 年 9 月