A quick glimpse of what's new in ES10
Here’s a quick glimpse of all the TS10 features, that are part of the spec and have already been shipped with the latest version of Chrome:
-
Trim whitespaces only from start of a string
" My String ".trimStart() // Produces: "My String "
-
Trim whitespaces only from end of a string:
" My String ".trimEnd() // Produces: " My String"
-
Flatten an array:
[1, 2, 3, [4, [5, 6]]].flat() // Produces: [1, 2, 3, 4, [5, 6]]
-
Flatten an array to depth of 2:
[1, [2, 3], 4, 5, [6, [7 , 8]]].flat(2) // Produces: [1, 2, 3, 4, 5, 6, 7, 8]
-
Flatten the whole array:
[1, 2, [3], [ [[4, 5, 6]], [[[[7, 8]], 9]] ]].flat(Infinity) // Produces: [1, 2, 3, 4, 5, 6, 7, 8, 9]
-
Create objects from array like representation of the object:
Object.fromEntries( [ ["key1", "value"], ["key2", 37] ] ) // Produces: Object { key1: "value", key2: 37 }
-
Create objects from a map:
Object.fromEntries( new Map([ ["key1", "value"], ["key2", 37] ]) ) // Produces: Object { key1: "value", key2: 37 }
-
Get symbol description from read-only property
.description
:const sym = Symbol('foo'); console.log(sym.description); // Outputs: foo
-
Catch without the once-mandatory parameter in catch block:
try { throw new Error("Some error"); } catch { console.log('An error occured'); } // Outputs: An error occured
-
Return source code of a function, verbatim, using
toString()
method:const fn = (param1, param2) => { console.log(param1, param2); } console.log(fn.toString()) // Outputs: (param1, param2) => { console.log(param1, param2); }
-
Return all matches (and capturing group) of a string against given regular expression:
const regexp = RegExp('l','g'); const str = 'Hello world'; let matches = str.matchAll(regexp); for (const match of matches) { console.log(match); } // Outputs: Array ["l"] Array ["l"] Array ["l"]
-
Map then flatten the array using
flatMap
[1, 2, 3, 4].flatMap(elem => [elem * 2]) // Produces: [2, 4, 6, 8]
Are these features ready to be used?
Well, some are and some are not. You can check for browser or nodejs compatibility by looking up features on MDN.
For example:
- Array.prototype.flat()[1] is available in NodeJS 11, Chrome 69 and Firefox 62 and above.
Nevertheless these are some really interesting additions to JavaScript and more is on the way.
Links and more:
[2] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt
[3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap