New JS features in Node.js v14.0

Sebastian Curland
Nielsen-TLV-Tech-Blog
3 min readApr 24, 2020
source: www.pikrepo.com

Node.js v14.0 was released this week, becoming the Current release. That means that in six months, by the end of October 2020, it will be moved to Active LTS (long-term-support) status and will be ready for general use.

Node.js v14.0.0 was updated to use V8 8.1, which brings support for the latest JS features. In this article, I would like to do a recap of those exciting features.

Optional Chaining

If you are a JS developer you probably ran into code where you need to check if an object property is null or undefined before accessing it, like in the following code:

if(user && user.details) {
const name = user.details.name
}

This type of code is not declarative, and it is very error-prone — I’m pretty sure you are already familiar with the error Cannot read property ‘xxx’ of undefined.
This also has the (unwanted) consequence of checking for all truthy values instead of checking only non-nullish values.

V8 version 8.0 introduced Optional Chaining and its operator ?. , so the above code can be rewritten to:

const name = user?.details?.name

What happens if user or details are undefined? JavaScript initializes name to undefined instead of throwing an error.

Other use-cases where Optional Chaining operator come in handy:

// user.details is deleted only if user is defined
delete user?.details
// Use operator ?.() to call optional methods
const adminOption = user?.prefs?.getPrefs?.().option
// Use operator ?.[] to access dynamic properties
const rolesCount = user?.preferences?.['roles'].length
// If usersArray is null or undefined then name will be undefined
const userIndex = 5
const name = usersArray?.[userIndex].name

Nullish Coalescing

A common pattern in Javascript is to use the || operator to set default values, like in the below example:

function convertUser(user) {
const isActive = user.active || true
}

But what would happen if user.active is false? Then the variable isActive will still be true, probably causing an unexpected behavior in our code.

V8 version 8.0 gives us the Nullish Coalescing operator ?? that can be used instead of || to fix the above bug:

const isActive = user.active ?? true

isActive will be true if user.active is null or undefined but will be false if user.active is falsy.

Optional Chaining + Nullish Coalescing

Nullish Coalescing can be used to set a default value when using optional chaining. In the below example name will be unknown if the user object does not have a name property inside details.

const name = user?.details?.name ?? 'unknown'

The last two features are related to the Intl module.

The Intl object is the namespace for the ECMAScript Internationalization API, which provides language sensitive string comparison, number formatting, and date and time formatting.

Intl.DisplayNames

The new Intl.DisplayNames API allows us to get translations of language, currency, and region names in different languages. For example, let’s say we want region names in Spanish, we can use the following code:

const regionNames = new Intl.DisplayNames(['es'], {type: 'region'})regionNames.of('FR') // 'Francia'
regionNames.of('US') // 'Estados Unidos'

Or if we want to get language names in French:

const langNames = new Intl.DisplayNames(['fr'], {type: 'language'})langNames.of('ES') // 'espagnol'
langNames.of('EN') // 'anglais'

And lastly, let’s see an example of getting currency names in Simplified Chinese:

const currencyNames = new Intl.DisplayNames(['zh-Hans'], {type: 'currency'});currencyNames.of('USD') // '美元'
currencyNames.of('EUR') // '欧元'

Intl.DateTimeFormat options

The Intl.DateTimeFormat API is used to work with language-specific date and time formatting. For example:

const date = new Date(Date.UTC(2012, 11, 20, 3, 0, 0))// British English uses day-month-year order
console.log(new Intl.DateTimeFormat('en-GB').format(date))
// "19/12/2012"

The date and time formats can be customized using the options argument in the DateTimeFormat constructor.

This API is not new and was added to V8 in the past, but now it supports calendar and numberingSystem options:

const options = {calendar: 'chinese', numberingSystem: 'arab'}
const dateFormat = new Intl.DateTimeFormat('default', options)

All the above features come with Node.js v14.0 by default. Also, the version includes other non-V8-related features that you can read about in the release notes. I would probably write more about them in the coming months.

Thanks for reading! I hope you enjoyed this article. Let me know if you have any comments or feedback.

Stay tuned for more Node.js articles and follow me on Twitter https://twitter.com/sebcurland

--

--

Sebastian Curland
Nielsen-TLV-Tech-Blog

Senior full-stack developer and security champion at the Nielsen Marketing Cloud.