ES7
The following features have been added to ES2016 (ES7)
Array.prototype.includes
includes()
method is used to determine whether an array or string contains a specified value
Return value: true
if included, false
otherwise.
Syntax
arr.includes(valueToFind)
arr.includes(valueToFind, fromIndex)
|
|
fromIndex
is greater than or equal to the array length
Return false
|
|
The calculated index is less than 0
If fromIndex
is negative, use the index calculated by arrlength + fromIndex as the new fromIndex . If the new fromIndex is negative, then Search the entire array.
|
|
Exponentiation Operator Power operation
Power operator ** , equivalent to Math.pow()
|
|
ES8
ES2017 (ES8) has added the following features
Async functions
Object.entries
Object.values
Object.getOwnPropertyDescriptors
Trailing commas
Async functions
Async functions
is a function declared by async
and the async
function is an instance of the AsyncFunction
constructor, where await
is allowed Keywords.
Return value: a Promise
Syntax
|
|
Example:
|
|
Object.entries
Return value: The Object.entries() method returns an array of key-value pairs for the given object’s own enumerable properties.
Syntax
|
|
Example:
|
|
Object.values
Return value: The Object.values() method returns an array of the enumerable property values ββof the given object itself.
Syntax
|
|
Example:
|
|
Object.getOwnPropertyDescriptors
Return value: The Object.getOwnPropertyDescriptors() method is used to obtain the descriptors of all its own properties of an object.
Syntax
|
|
Example:
|
|
Trailing commas
If you want to add a new attribute, and the previous line already used a trailing comma, you can just add the new line without modifying the previous line.
JSON does not allow trailing commas
Example:
- Trailing comma in literal
|
|
- Trailing comma in function
|
|
- Trailing comma in deconstruction
|
|
String.prototype.padStart()
padStart()
Fills the current string with another string.
Return value: A new string formed by filling the specified padding string at the beginning of the original string until the target length.
Syntax
|
|
targetLength: The target length to which the current string needs to be filled. If this value is less than the length of the current string, the current string itself is returned.
padString (optional): padding string. If the string is too long and the padded string length exceeds the target length, only the leftmost part is retained and the other parts are truncated. The default value for this parameter is β β.
Example:
|
|
String.prototype.padEnd()
The padEnd()
method fills the current string with a string (repeatedly if necessary).
Return value: Returns a new string formed by filling the specified padding string at the end of the original string until the target length.
Syntax
|
|
targetLength: The target length to which the current string needs to be filled. If this value is less than the length of the current string, the current string itself is returned.
padString (optional): padding string. If the string is too long and the padded string length exceeds the target length, only the leftmost part is retained and the other parts are truncated. The default value for this parameter is β β.
Example:
|
|
ES9
ES2018 (ES9) has added the following features
Async iterators
Object rest properties
Object spread properties
Promise.prototype.finally
Async iterators
Return value: The next() method of Async iterator object returns a Promise . The return value of this Promise can be parsed into the format of {value, done}
Syntax
|
|
Example:
|
|
You can use for-await-of to call functions asynchronously in a loop
|
|
Object rest properties
Example:
|
|
Note: null Cannot use spread operator
Object spread properties
Example:
|
|
Promise.prototype.finally
At the end of Promise , whether the result is resolved or rejected , the method in finally will be called
The callback function in finally does not accept any parameters
Return value: a Promise
Syntax
|
|
Example:
|
|
ES10
ES2019 (ES10) has added the following new features
Array.prototype.{flat, flatMap}Flatten nested arrays
Object.fromEntries
String.prototype.{trimStart, trimEnd}
Symbol.prototype.description
Optional catch binding
Array.prototype.sort() is now required to be stable
Array.prototype.{flat, flatMap} Flatten nested arrays
Array.prototype.flat
The flat()
method will traverse the recursive array according to a specifiable depth, and merge all elements with the elements in the traversed sub-array into a new array and return it.
Return value: a new array, the old array will not be changed.
Syntax
|
|
Example:
|
|
Note: flat()
will remove empty items from the array
|
|
Implement flat
|
|
Array.prototype.flatMap
The flatMap()
method first maps each element of the array (with a depth value of 1) using the mapping function, and then compresses the result into a new array.
Return value: a new array with each element being the result of the callback function.
Syntax
|
|
callback: a function called to generate a new array
currentVal: The element currently being processed by the array
index: optional, the index of the element being processed
array: optional, the array to be called
thisArg: this value used when executing the callback function
Example:
|
|
Object.fromEntries
The fromEntries()
method converts a list of key-value pairs into an object
Return value: a new object
Syntax
|
|
iterable: Array, Map and other iterable objects
Example:
|
|
String.prototype.{trimStart, trimEnd}
String.prototype.trimStart
The trimStart()
method is used to remove whitespace characters at the beginning of a string.
trimLeft()
is its alias.
Return value: a new string with the spaces on the left side of this string removed.
Syntax
|
|
Example:
|
|
String.prototype.trimEnd
The trimEnd()
method is used to remove whitespace characters at the end of a string.
Return value: a new string, the spaces on the right side of this string have been removed
Syntax
|
|
Example:
|
|
Symbol.prototype.description
description is a read-only property
Return value: It returns a string with an optional description of the Symbol object
Syntax
|
|
Example:
|
|
Optional catch binding
Optional capture binding, allowing omission of the catch binding and the parentheses following it
Previous usage:
|
|
ES10 usage:
|
|
The strengthening power of JSON.stringify()
JSON.stringify() In ES10, the problem of display errors for some out-of-range Unicode has been fixed. Therefore, characters within 0xD800-0xDFF will cause display errors because they cannot be encoded into UTF-8. In ES10, it will use escape characters to process these characters instead of encoding, so they will be displayed normally.
|
|
Revision Function.prototype.toString()
The previous toString method from Object.prototype.toString()
is now a Function.prototype.toString()
method that returns a string representing the source code of the current function. Previously, only this function would be returned, without spaces, comments, etc.
|
|
ES11
ES2020 (ES11) has added the following new features
Nullish coalescing Operator
Optional chaining
globalThis
BigInt
String.prototype.matchAll()
Promise.allSettled()
Dynamic import (import on demand)
Nullish coalescing Operator
Null value merging operator ( ?? )
The null value merging operator ( ??
) is a logical operator that returns the right operator when the left operand is null or undefined , otherwise returns the left-hand operator.
|
|
Logical OR operator ( || )
The logical OR operator ( || ) will return the right operand when the left operand is false, that is, if you use || to set default values ββfor some variables , unexpected situations may occur. For example, 0, ‘’, NaN, false:
|
|
Note
You cannot use ?? with AND ( && ) OR ( || ), and an error will be reported.
|
|
Optional chaining Optional chaining
The optional chain operator ( ?. ) allows reading the value of a property located deep in the chain of connected objects without having to explicitly verify that each reference in the chain is valid. The ?. operator functions similarly to the . chaining operator, except that when referenced as null or undefined An error will be reported, and the return value of the link expression is undefined .
Previous writing:
|
|
ES11:
|
|
Note: Optional chains cannot be used for assignment:
|
|
globalThis
Previously, in the Web, global objects could be obtained through window and self , but in node.js, global must be used.
In loose mode, you can return this in a function to get the global object, but in strict mode and module environment, this will return undefined .
In the past, to obtain the global object, you could define a function:
|
|
globalThis now provides a standard way to obtain the global object’s own value in different environments.
BigInt
BigInt is a built-in object used to create integers larger than 2β΅Β³ β 1 (the largest number that can be created by Number). Can be used to represent arbitrarily large integers
How to define a BigInt
Add n after an integer literal, for example 10n
Call function BigInt() and pass an integer or string value, such as BigInt(10)
Features of BigInt
BigInt cannot be used in methods in Math objects;
BigInt cannot be mixed with any Number instance, both must be converted to the same type. Note, however, that BigInt may lose precision when converted to Number.
When using BigInt, operations with decimals are rounded down
BigInt and Number are not strictly equal, but are loosely equal
|
|
- BigInt and Number can be compared
|
|
- BigInt and Number can be mixed in an array for sorting
|
|
- BigInt wrapped by Object is compared using the comparison rules of object, and is only equal when comparing with the same object.
|
|
BigInt methods
BigInt.asIntN() β Converts a BigInt value to a signed integer between -2^(width-1) and 2^(width-1) β 1.
BigInt.asUintN() β Converts a BigInt value to an unsigned integer between 0 and 2^(width) β 1.
BigInt.prototype.toLocaleString() β Returns the language-sensitive form of this number as a string. Override the Object.prototype.toLocaleString() method.
BigInt.prototype.toString() β Returns a string representing the specified number in the specified base. Override the Object.prototype.toString() method.
BigInt.prototype.valueOf() β Returns the primitive value of the specified object. Override the Object.prototype.valueOf() method.
Why is there a Bigint proposal?
In JavaScript, Number.MAX_SAFE_INTEGER represents the maximum safe number, and the calculated result is 9007199254740991, that is, there will be no loss of precision within this number range (except for decimals). But once it exceeds this range, js will have inaccurate calculations, which has to rely on some third-party libraries to solve large number calculations. Therefore, the official proposed BigInt to solve this problem.
String.prototype.matchAll()
Returns an iterator containing all results matching the regular expression and grouped capturing groups.
|
|
Promise.allSettled()
Class method that returns a promise after all given promises have been fulfilled or rejected, with an array of objects, each object representing the corresponding promise result.
|
|
Dynamic import (import on demand)
import You can load a module when needed.
|
|
ES12
ES 2021 (ES12) has added the following new features
Logical operators and assignment expressions (&&=, ||=, ??=)
String.prototype.replaceAll()
Number separator
Promise.any
Logical operators and assignment expressions (&&=, ||=, ??=)
- &&=
The logical AND assignment operator x &&= y is equivalent to x && (x=y) : meaning that when x is true, x = y.
|
|
- ||=
The logical OR assignment operator x ||= y is equivalent to x || (x = y) : meaning x = y only if x is false.
|
|
- ??=
The logical null assignment operator x ??= y is equivalent to x ?? (x = y) : meaning that x = y only if x is null or undefined.
|
|
String.prototype.replaceAll()
Returns a new string in which all parts of the string that match pattern will be replaced by replacement. The original string remains unchanged.
pattern can be a string or RegExp;
replacement can be a string or a function that is called each time it is matched.
|
|
When searching for a value using a regular expression, it must be global:
|
|
Number separator
ES12 allows JavaScript values ββto use underscores (_) as separators, but does not specify the number of digits to separate them:
|
|
Decimal and scientific notation can also use delimiters:
|
|
NOTE:
Cannot be placed at the front or last of the value;
Two or more delimiters cannot be connected together;
There cannot be separators before and after the decimal point;
In scientific notation, there cannot be separators before or after e or E.
Promise.any
The method accepts a set of Promise instances as parameters, wraps them into a new Promise instance and returns it.
As long as one parameter instance becomes fulfilled, the wrapper instance will become fulfilled; if all parameter instances become rejected, the wrapper instance will become rejected.
|
|