close
The Wayback Machine - https://web.archive.org/web/20210124025139/https://medium.com/better-programming/twelve-es10-features-in-twelve-simple-examples-6e8cc109f3d3

Twelve ES10 Features in Twelve Simple Examples

New ES10 features step-by-step

Carlos Caballero
Sep 23, 2019 · 5 min read
Image for post
Image for post

Today, we’re going to look at ECMAScript features from 2015:

ES10 is the version of ECMAScript corresponding to the year 2019. This version does not include as many new features as the ES6 release (2015). However, some useful features have been incorporated.

This article introduces the features provided by ES10 in easy code examples. In this way, you can quickly understand the new features without the need for a complex explanation.

Of course, it is necessary to have a basic knowledge of JavaScript to fully understand the best ones introduced.

The new JavaScript features in ES2019 are:

  • Array#{flat,flatMap}
  • Object.fromEntries
  • String#{trimStart,trimEnd}
  • Symbol#description
  • try { } catch {} // optional binding
  • JSON ECMAScript
  • well-formed JSON.stringify
  • stable Array#sort
  • revised Function#toString
  • BigInt primitive type (stage 3)
  • Dynamic import (stage 3)
  • Standardized globalThis object (stage 3)

Array.flat() and Array.flatMap()

There are two new Array methods:

  • The Array.flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.
  • The Array.flatMap() method first maps each element using a mapping function, then flattens the result into a new array. It is identical to a map() followed by a flat() of depth 1, but flatMap() is often quite useful, as merging both into one method is slightly more efficient.
Image for post
Image for post

Object.fromEntries()

Transform a list of key and value pairs into an object.

Image for post
Image for post

String.protype.matchAll

The matchAll() method returns an iterator of all results matching a string against a regular expression, including capturing groups.

Image for post
Image for post

String.trimStart() and String.trimEnd()

There are two new String methods to remove whitespace from a string:

  • The trimStart() method removes whitespace from the beginning of a string.
  • The trimEnd() method removes whitespace from the end of a string.
Image for post
Image for post

Symbol.Description

There is a new Symbol description accessor. When you create a Symbol you can now provide a string as a description; in ES10 there is an accessor to this property.

Image for post
Image for post

Optional Catch Binding

In the past, catch clause from a try/catch statement required a variable. Now developers can use try/catch without creating an unused binding.

Image for post
Image for post

JSON ⊂ ECMAScript

The unescaped line separator U+2028 and paragraph separator U+2029 characters were not accepted in the pre-ES10 era.

  • U+2028 is the paragraph separator.
  • U+2029 is the line separator.
Image for post
Image for post

Well-Formed JSON.stringify()

JSON.stringify() may return characters between U+D800 and U+DFFF as values for which there are no equivalent UTF-8 characters. However, JSON format requires UTF-8 encoding. The proposed solution is to represent unpaired surrogate code points as JSON escape sequences rather than returning them as single UTF-16 code units.

Image for post
Image for post

Stable Array.prototype.sort()

The previous implementation of V8 used an unstable quick-sort algorithm for arrays containing more than 10 items.

A stable sorting algorithm is when two objects with equal keys appear in the same order in the sorted output as they appear in the unsorted input.

Image for post
Image for post

New Function.toString()

The toString() method returns a string representing the source code of the function. In ES6, when toString was invoked on a function it would return string representation of that function, depending on the ECMAScript engine. When possible, it would return the source code, otherwise it would return a standardized placeholder.

Image for post
Image for post

BigInt — Arbitrary Precision Integers

BigInt is the 7th primitive type: an arbitrary precision integer. The variables can now represent 253 numbers and not just max out at 9007199254740992.

Image for post
Image for post

Dynamic Import

Dynamic import() returns a promise for the module namespace object of the requested module. Therefore, imports can now be assigned to a variable using async/await.

Image for post
Image for post

Standardized globalThis object

The globalThis object was not standardized before ES10. In production code you would “standardize” it across multiple platforms on your own by writing this monstrosity:

Image for post
Image for post

If you liked this article and would like to read similar articles, don’t forget to clap.

Image for post
Image for post
Click and drag to clap more than once. 50 is the limit.

Conclusion

JavaScript is a living language, and that’s very healthy for web development. Since the appearance of ES6 in 2015, we’ve been seeing a vibrant evolution in the language. In this post, we reviewed the features that arise in ES10 (2019) and introduced some that will be stable in ES11 (2020), since they are in state 3 and will probably end up being standardized for the next edition.

Although many of these features may not be essential for the development of your web application, they give capabilities that couldn’t be achieved before without tricks or a lot of verbosity.

Advice for programmers.

By Better Programming

A weekly newsletter sent every Friday with the best articles we published that week. Code tutorials, advice, career opportunities, and more! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Carlos Caballero

Written by

Hi! My name is Carlos Caballero and I’m PhD. in Computer Science from Málaga, Spain. Teaching developers and degree/master computer science how to be experts!

Better Programming

Advice for programmers.

Carlos Caballero

Written by

Hi! My name is Carlos Caballero and I’m PhD. in Computer Science from Málaga, Spain. Teaching developers and degree/master computer science how to be experts!

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store