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).
BigInt in JavaScript
JavaScript BigInt variables are used to store large integer values that cannot be represented by ordinary JavaScript numbers.
JavaScript integers can only be precise up 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 support BigInt:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 67 | Edge 79 | Firefox 68 | Safari 14 | Opera 54 |
May 2018 | January 2020 | July 2019 | Σεπτέμβριος 2020 | June 2018 |
Μέθοδος matchAll() για字符串
Before ES2020, there were no string methods 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 ??
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 | Μάρτιος 2020 | Μάρτιος 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 ?.=
Αντιγραφές:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 80 | Edge 80 | Firefox 74 | Safari 13.1 | Opera 67 |
February 2020 | February 2020 | Μάρτιος 2020 | Μάρτιος 2020 | Μάρτιος 2020 |
Λογικός AND όρισμα αποθήκευσης (&&= όρισμα)
Logical AND Assignment Operatorused between two values.
If the first value is true
Αν δεν είναι διαθέσιμος ο πρώτος όρος, τότε θα αποθηκευτεί ο δεύτερος όρος.
Logical AND Assignment Example
let x = 100; x &&= 5;
Από τον Σεπτέμβριο του 2020, όλα τα σύγχρονα προγράμματα περιήγησης υποστηρίζουν &&=
Αντιγραφές:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
Αύγουστος 2020 | Αύγουστος 2020 | Μάρτιος 2020 | Σεπτέμβριος 2020 | Σεπτέμβριος 2020 |
Logical OR Assignment Operator (||= Operator)
Logical OR Assignment Operatorused between two values.
If the first value is false
Αν δεν είναι διαθέσιμος ο πρώτος όρος, τότε θα αποθηκευτεί ο δεύτερος όρος.
Logical OR Assignment Example
let x = 10; x ||= 5;
Από τον Σεπτέμβριο του 2020, όλα τα σύγχρονα προγράμματα περιήγησης υποστηρίζουν ||=
Αντιγραφές:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
Αύγουστος 2020 | Αύγουστος 2020 | Μάρτιος 2020 | Σεπτέμβριος 2020 | Σεπτέμβριος 2020 |
Σύμπλοκο όρισμα αποθήκευσης (??= όρισμα)
Σύμπλοκο όρισμα αποθήκευσης(Nullish Coalescing Assignment Operator)χρησιμοποιείται μεταξύ δύο τιμών.
Αν ο πρώτος όρος undefined
ή null
Αν δεν είναι διαθέσιμος ο πρώτος όρος, τότε θα αποθηκευτεί ο δεύτερος όρος.
Παράδειγμα συμπλοκού όρισμα αποθήκευσης
let x = 10; x ??= 5;
Από τον Σεπτέμβριο του 2020, όλα τα σύγχρονα προγράμματα περιήγησης υποστηρίζουν ??=
Αντιγραφές:
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome 85 | Edge 85 | Firefox 79 | Safari 14 | Opera 71 |
Αύγουστος 2020 | Αύγουστος 2020 | Μάρτιος 2020 | Σεπτέμβριος 2020 | Σεπτέμβριος 2020 |