ECMAScript 2020
JavaScript version number
Old JS versions are named by number: ES5 (2009) and ES6 (2015).
Starting from 2016, versions are named by year: ECMAScript 2016, 2017, 2018, 2019, ...
New features in ES2020:
- BigInt
- String method matchAll()
- Nullish Coalescing Operator (??)
- Optional Chaining Operator (?.)
- Logical AND Assignment Operator (&&=)
- Logical OR Assignment Operator (||=)
- Nullish Coalescing Assignment Operator (??=)
- Promise.allSettled()
- Dynamic Import
Warning
These features are relatively new.
Older browsers may require alternative code (Polyfill).
JavaScript BigInt
JavaScript BigInt variables are used to store large integer values that cannot be represented by ordinary JavaScript numbers.
JavaScript integers can only be accurate to 15 digits.
Integer instance
let x = 999999999999999; let y = 9999999999999999; // Too large
BigInt instance
let x = 9999999999999999; let y = 9999999999999999n;
To create a BigInt, please use n
Attach to the end of an integer or call BigInt():
Example
let x = 1234567890123456789012345n; let y = BigInt(1234567890123456789012345)
The JavaScript type of BigInt is "bigint":
Example
let x = BigInt(999999999999999); let type = typeof x;
Since September 2020, all modern browsers have supported BigInt:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 67 | Edge 79 | Firefox 68 | Safari 14 | Opera 54 |
May 2018 | January 2020 | July 2019 | September 2020 | June 2018 |
JavaScript String Method 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 nullish value (null
or undefined
),then ??
The 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 | March 2020 | March 2020 |
Optional Chaining Operator (Optional Chaining Operator) (?. Operator)
If the object is undefined
or null
thenOptional 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 ?.=
Operator:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 80 | Edge 80 | Firefox 74 | Safari 13.1 | Opera 67 |
February 2020 | February 2020 | March 2020 | March 2020 | March 2020 |
Logical AND Assignment Operator (&&= Operator)
Logical AND Assignment Operatorused between two values.
If the first value is true
If the first value is null or undefined, then assign the second value.
Logical AND Assignment Example
let x = 100; x &&= 5;
All modern browsers have supported since September 2020 &&=
Operator:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
August 2020 | August 2020 | March 2020 | September 2020 | September 2020 |
Logical OR Assignment Operator (||= Operator)
Logical OR Assignment Operatorused between two values.
If the first value is false
If the first value is null or undefined, then assign the second value.
Logical OR Assignment Example
let x = 10; x ||= 5;
All modern browsers have supported since September 2020 ||=
Operator:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
August 2020 | August 2020 | March 2020 | September 2020 | September 2020 |
Nullish Coalescing Assignment Operator (??= Operator)
Nullish Coalescing Assignment Operator(Nullish Coalescing Assignment Operator) is used between two values.
If the first value undefined
or undefined null
If the first value is null or undefined, then assign the second value.
Nullish Coalescing Assignment Example
let x = 10; x ??= 5;
All modern browsers have supported since September 2020 ??=
Operator:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
August 2020 | August 2020 | March 2020 | September 2020 | September 2020 |