ES8 What’s New in JavaScript?

Currently, my day-to-day life is fully engrossed with coding. Specifically, I am going through an all-time consuming immersive program to become a software engineer. JavaScript, being my base language, recently received new specifications, or features, via the release of ES8 about six months ago in June 2017. I think it’s worth while to highlight what’s new!

String Padding

There are two new functions on the String Object: padStart and padEnd. Both functions take two arguments, a number and an (optional) string, and return a string with a minimum length of number (hereinafter referred to as minLength). The number represents the “minimum length” of the returned string. If a string’s length is less than minLength then padStart adds “padding” at the beginning of the string until the string’s length equals minLength. Calling padEnd under the same circumstance adds “padding” to the end of the string until the string’s length equals minLength. The “padding” by default is blank spaces. However, you can provide a string as the second argument and the characters of the string will be used to pad the string sequentially, potentially repeating characters if necessary or not including all characters depending on when the desired length of the returned string is reached. Here are some simple examples to illustrate syntax and return values of padStart and padEnd:

Object.values(obj) & Object.entries(obj)

Similar to Object.keys(obj), except Object.values(obj) returns an array of the given object’s (obj) values. And Object.entries(obj) returns an array of arrays with each sub-array containing a key/value pair: [ key, value]. [key, value] ].

Trailing Commas

An error will no longer be thrown when there is a trailing comma at the end of a function’s parameter or arguments list.

Object Descriptors

Object.getOwnPropertyDescriptors(obj, prop) takes an object and a property and returns all property descriptors of the given property. Object.getOwnPropertyDescriptors allows a programmer to pass down a property’s descriptor values through prototypal inheritance. Specifically when using Object.create. For instance:

Prior to ES8, Object.create would reset all descriptors to their default values. Now, creating an object with the syntax from line 9 would not allow the new object to set a name property. The name property would also delegate up to person and return ‘Sally” unless the writable descriptor is reassigned to true.

The return value is an object with the following properties: “value”, “writable”, “get”, “set”, “configurable”, and “enumerable”.

“value” is the value of the associated property (prop).

“writable” is a boolean and is true if the value of prop can be changed.

“get” and “set” are functions that serve as the getter/setter functions of prop respectively, or undefined if prop does not have getter/setter functions.

“configurable” is a boolean and if true the descriptor properties can be changed and prop can be modified/deleted from the corresponding object (obj).

“enumerable” is true if the prop will be accounted for/looped-over when a for in loop is invoked on the corresponding object (obj).

Shared Array Buffer Object

Gives the programmer the ability to control memory allocation and deallocation. Using the keyword new you can assign an array of a set size (in bytes).

newArray has a set size in memory of 10 bytes.

The use case is for narrow/specific situations when using memory space as efficiently as possible is absolutely vital. JavaScript, when allocating memory for complex datatypes often reserves/uses more memory space than is actually needed. Also, garbage collection can be inefficient at times when unimportant references are left pointing at unneeded variables. The SharedArrayBuffer offers more control over an applications use of memory space as an alternative.

Async Functions

The evolution of “asynchronous” JavaScript and Promises continue. Async functions seeks to add more syntactically pleasing options to Promises (which in turn did the same for “callback hell”). In front of a function marked async a programmer can place the await keyword in front of an expression that returns a promise. The execution of the async function is paused until the promise is resolved.

It is important to note that only one await happens at a time. That is, if there were more awaits on lines 4, 5, etc… then they would not execute until the first await on line 3 is resolved. If you want to await more than one promise at the same time then you must use await Promise.all();