JS 2019 features I am excited about
Hozefa

Hozefa @hozefaj

About: Engineering Manager prev @Meta | @Wealthfront | @PayPal

Joined:
Feb 15, 2019

JS 2019 features I am excited about

Publish Date: Aug 9 '19
10 2

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.

Comments 2 total

  • Jacob Evans
    Jacob EvansAug 10, 2019

    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.

    • Hozefa
      HozefaAug 10, 2019

      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.

Add comment