DEV Community

Hozefa
Hozefa

Posted on

3 2

JS 2019 features I am excited about

TC39 over the past few years have been giving yearly updates to the JS. This works much better than throwing a ton of updates very years. This gives developers an opportunity to learn in a better and even from the browser point of view making the features available.

From the 2019 feature set, below are a few that I am most excited about.

1. trimStart() and trimEnd()

Added on the String prototype

This allows trimming of whitespace at the start and end of a string. We already have the trim() function but this does it at both ends. There can be a use case where you might keep the whitespace at either end.

const paddedStr = ' How new trim functions work '
paddedStr.trimStart() // prints 'How new trim functions work '
paddedStr.trimEnd() // prints ' How new trim functions work'
paddedStr.trim() // prints 'How new trim functions work'

Enter fullscreen mode Exit fullscreen mode

2. flat()

Added on the Array prototype

This helps with flattening of multi-dimensional arrays. It takes an optional parameter that tells the function how many levels deep we want to flatten. The default value is 1.

const oneLevelNesting = ['bread', ['peanut butter', 'jelly'], 'bread'];
oneLevelNesting.flat() // ['bread', 'peanut butter', 'jelly', 'bread']

const twoLevelNesting = ['bread', [['peanuts', 'oil'], ['strawberries', 'sugar']], 'bread'];
twoLevelNesting.flat() //  ['bread', ['peanuts', 'oil'], ['strawberries', 'sugar'], 'bread']
twoLevelNesting.flat(2) // ['bread', 'peanuts', 'oil', 'strawberries', 'sugar', 'bread']

Enter fullscreen mode Exit fullscreen mode

3. Optional catch binding

Optional catch binding allows you to write a try/catch without having to use the error parameter with catch.

//before
try {
   doSomething()
} catch(error) {
   console.log("Not using the error parameter here but have to include it")
}
// after
try {
   doSomethingElse()
} catch {
   console.log("No param? No Problem!")
}
Enter fullscreen mode Exit fullscreen mode

There are other features other than these like flapMap(), Object.fromEntries, another one is adding a description to Symbol property. Personally, I have not found the need for these in my experience.

For a deeper understanding of all these features I recommend watching javascript-es2019-in-practice on egghead.io.

Would love to hear thoughts of others on what features are they most excited about.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (2)

Collapse
 
jacobmgevans profile image
Jacob Evans β€’

How are you feeling about the optional chaining and null coalescing operator.

I am super excited about those moving up in stages as well. :)

with flat, I am looking forward to .flatMap() as well.

Collapse
 
hozefaj profile image
Hozefa β€’ β€’ Edited

flatMap() might be useful. In most cases I rather feel I would chain map() followed by flat() to keep the logic in map separate from flattening the array.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay