As one of the most popular programming languages and the staple of web development, JavaScript is constantly evolving and with each iteration it gets some fresh internal changes. Let’s check out some of the new proposed features of ES2019 that could soon find a way into our everyday code:

You can now flatten nested arrays recursively up to a specified depth. The default value is 1 and if you want to go full depth use Infinity. This method does not modify the original array but creates a new one:

const arr1 = [ 1 , 2 , [ 3 , 4 ] ] ; arr1 . flat ( ) ; const arr2 = [ 1 , 2 , [ 3 , 4 , [ 5 , 6 ] ] ] ; arr2 . flat ( 2 ) ; const arr3 = [ 1 , 2 , [ 3 , 4 , [ 5 , 6 , [ 7 , 8 ] ] ] ] ; arr3 . flat ( Infinity ) ;

If you have an empty slot in your array it is going to be removed:

const arr4 = [ 1 , 2 , , 4 , 5 ] ; arr4 . flat ( ) ;

A new method that combines the basic map function and then flattens the result to a depth of 1 with the new Array.flat() method:

const arr1 = [ 1 , 2 , 3 ] ; arr1 . map ( x => [ x * 4 ] ) ; arr1 . flatMap ( x => [ x * 4 ] ) ;

Another more useful example:

const sentence = [ "This is a" , "regular" , "sentence" ] ; sentence . map ( x => x . split ( " " ) ) ; sentence . flatMap ( x => x . split ( " " ) ) ;

String.trimStart() and String.trimEnd()

In addition to String.Trim() which removes whitespaces from both sides of a string there are now separate methods for only removing white spaces from each side:

const test = " hello " ; test . trim ( ) ; test . trimStart ( ) ; test . trimEnd ( ) ;

A new method that transforms a list of key-value pairs into an object. It performs the reverse of an already familiar function Object.Entries which is used when transforming objects to arrays for their easier manipulation. After the transformation, you would be left with an array but now you can return the manipulated array back into an object. Let’s try with an example where we want to square the values of all of our object properties:

const obj = { prop1 : 2 , prop2 : 10 , prop3 : 15 } ; let array = Object . entries ( obj ) ;

Let’s square the values of the new list of key-value pairs with a simple map:

array = array . map ( ( [ key , value ] ) => [ key , Math . pow ( value , 2 ) ] ) ;

We’ve transformed the object values but we are left with an array and that is where Object.fromEntries comes in, transforming the array back to an object:

const newObj = Object . fromEntries ( array ) ;

Optional Catch Binding

The new proposal allows you to completely omit the catch() parameter as there are a lot of cases where you don’t want to use it:

try { } catch ( er ) { } try { } catch { }

You can now access the description property of a Symbol instead of using the toString() method:

const testSymbol = Symbol ( "Desc" ) ; testSymbol . description ;

Calling toString() on a function now returns the function exactly as it was defined including whitespaces and comments. Before we had:

function foo ( ) { } foo . toString ( ) ;

And now it’s:

foo . toString ( ) ;

JSON.parse() improvements

The line separator (\u2028) and paragraph separator (\u2029) symbols now parse correctly instead of resulting in a SyntaxError.