10th release of the ECMAScript language specification (ES2019)

The optional chain operator (? .).

Allows you to read the value of a property located deep in the chain of connected objects without explicitly validating that each reference in the chain is valid. ? The. Operator functions like the. Chain operator, except that it does not cause errors when references are nullish (null or undefined), and the short-circuit returns undefined.

const obj = {
  name: 'zhangsan'.cat: {
    name: 'li'
  },
  say() {
    console.log('hhh')}};constdogName = obj.dog? .name;console.log(dogName); // undefined

console.log(obj.cat? .name)// li
console.log(obj? .say())// 'hhh'
console.log(obj? .say1())TypeError: obj? TypeError: obj? .say1 is not a function
console.log(obj? .say1? . ()// undefined
Copy the code

BigInt

BigInt is a numeric type of data that can represent an integer in any precision format. In other programming languages, there can be different numeric types, such as integers, floating point numbers, double numbers, or Large wave numbers.

let n = BigInt(100)
typeof n // "bigint"
n === 100 // false
n == 100 // true
Copy the code

String.prototype.trimStart() / String.prototype.trimEnd()

Trim () is used to trim trim(), string.trimstart () is used to trim trim(), and string.trimend () is used to trim trim()

let str = ' asdfg ';
str.trimStart(); // 'asdfg '
str.trimEnd(); // ' asdfg'
Copy the code

Object.fromEntries()

ES8 introduces object. entries to convert an Object to a [key, value] key-value pair, which can be applied to structures like Map and object.fromentries () to restore key-value pairs to an Object structure

let arr = [['foo'.'bar']].Object.fromEntries(arr) // {foo: "bar"}

let obj = {a: 1.b: 2}
Object.entries(obj) // [["a", 1], ["b", 2]]
Copy the code

Array.prototype.flat() / Array.prototype.flatMap()

Flattening arrays is a new feature introduced by the Array prototype, which raises the level of the underlying Array by passing in the level depth parameter (default: 1). You can write a large number or even Infinity if you want to raise all levels, but this is not recommended

let arr = [1.2.3[4.5.6[8.9]], 7];

arr.flat(1) // [1, 2, 3, 4, 5, 6, [8, 9], 7]
arr.flat(2) // [1, 2, 3, 4, 5, 6, 8, 9, 7]
arr.flat(Infinity) // [1, 2, 3, 4, 5, 6, 8, 9, 7]
Copy the code

Array.prototype.flatmap () is a combination of array.prototype.map () and array.prototype.flat ()

[1.2[3.4]].flatMap(v= > {
  if (typeof v === 'number') {
    return v * 2
  } else {
    return v.map(v= > v * 2)}})// [2, 4, 6, 8]
Copy the code

The catch argument is changed to optional

In the try… During a catch error, if no argument is passed to the catch, the code will report an error. The parameters and parentheses of the catch binding can be omitted from the new specification

const isValid = params= > {
  try {
    return true;
  } catch {
    return false; }};Copy the code

Symbol.prototype.description

Description is a read-only property that returns a string of optional descriptions of the Symbol object.

let a = Symbol(123);

console.log(a) // Symbol(123)
typeof a // "symbol"

a.description / / "123"
typeof a.description // "string"
Copy the code

Dynamic import

Dynamic import (return Promise). Import commands are statically parsed by the JS engine, executed before other statements in the module, and cannot replace the dynamic loading function of require(). The proposal proposes to introduce import() instead of require(), where require() is synchronous loading and import() is asynchronous loading and is used in scenarios such as on-demand loading, conditional loading, and dynamic module paths

const loadJs = async (num) => {
	if (num === 1) {
		const a = await import('.. /xxx.js')
	}
}
loadJs(1)
Copy the code

Promise.allSettled()

The promise.allSettled () method returns a Promise after all the given promises have fulfilled or rejected, with an array of objects, each representing the corresponding Promise result.

const promise1 = Promise.resolve(3);
const promise2 = new Promise((resolve, reject) = > setTimeout(reject, 100.'foo'));
const promises = [promise1, promise2];

Promise.allSettled(promises).
  then((results) = > results.forEach((result) = > console.log(result.status)));

// "fulfilled"
// "rejected"
Copy the code

Reference: MDN