New features of ECMAScript 2017
Recently, the specifications of ECMAScript 2017 was released. Yes, it is called ECMAScript 2017 and not ES8. The naming convention started with ES1 and went through 1, 2, 3, 5, 5.1, 6 but ES6 was later renamed to ECMAScript 2015. Subsequent releases are called ECMAScript 2016 and 2017.
Enough with the nomenclature, lets get into the new features that are supported with this specifications.
- Object.entries
- Object.values
- String padding
- Object.getOwnPropertyDescriptors
- Trailing commas in function parameter lists and calls
- Async functions
- Exponentiation Operator
- Array.prototype.includes
Let us look at some detailed explanation for each of these
Object.entries
A new entries method has been included to the Object. When a JavaScript datatype with key value pairs is passed to the entries method, the return value is a two dimensional array of keys and values.
Object.values
The values method works similar to its predecessor but instead of returning both the keys and values, it returns a single dimension array of the values
String padding
Two methods padStart
and padEnd
has been added to String to support the padding at the beginning and the end of the string. Following are the definition and the examples of the methods
Def: String.prototype.padStart( maxLength [ , fillString ] )
Def: String.prototype.padEnd( maxLength [ , fillString ] )
Object.getOwnPropertyDescriptors
Object.getOwnPropertyDescriptors(obj) accepts an object obj and returns the property descriptors describing the attributes of a property (its value, whether it is writable, etc.)
In the above example, obj has a integer value assigned to id and a getter method with the name bar. So the getOwnPropertyDescriptors
returns the value along with other properties like writable, enumerable, configurable, getter, setter.
Trailing commas in function parameter lists and calls
Going forward object literals, array literals and function parameters allow a trailing comma
Why is this important? This solves problems like multi line commits to add a new parameter, rearranging items is simpler, because you don’t have to add and remove commas if the last item changes its position.
Async functions:
The usage of promises and generators has gained popularity. Though our code has been revived from the callback hell, the multiple then clusters can be simplified more and the code can be much more beautiful and legible.
Exponentiation Operator
Gone are the days when you had to depend on JavaScript’s Math object to perform exponential operation. ECMAScript 2017 introduces **
operator for the same
You can also perform x **= y for directly assigning the value to the variable x
Array.prototype.includes
This is a feature which must have been included in JavaScript years ago since validations like array.indexOf(element) !== -1
, array.indexOf(element) >0
, ~array.indexOf(element)
has been haunting every JS codebase. Instead of checking whether the element exists in the array, we are checking for the index of the first occurrence of the element in the array. Additionally, this would fail for NaN (eg: [NaN].indexOf(NaN) === -1
)
To overcome this, ECMAScript 2017 has introduced includes method to the Array to check whether an element is present in the Array. Henceforth to check the existence of an element, you can use the following
array.includes(element)
These are some of the majorly accepted proposals but there are tonnes of interesting proposals in Stage 2 and 3 assuring promising releases ahead.
Availability:
The spec is available by default in Chrome and Edge 16(Chakra) while firefox will be following them very soon.
**EDIT**
Exponentiation Operator and Array.prototype.includes have been a part of ECMAScript 2016 specification itself.