You are on page 1of 1

Amar 7 Followers About Follow Sign in Get started

Amar
Lead NodeJs developer and
solution provider.

Follow Amar Jul 8 · 4 min read

Javascript New Features | ES8(ECMA 2017) features


67 1
In my previous story, we have gone through ES7 features and NodeJs
compatibility. Now, It’s time to look at ES8 features. So, lets embark
exploring es8 features.

ES8

New features

As Javascript growing ES8 is definitely plays a key role to make it as a game


changer. Lets go through new features introduced in

Object.values

Object.entries

String.prototype.padStart

String.prototype.padEnd

Trailing commas in the function parameters

async/await

Object.getOwnPropertyDescriptors

Object.values

The Object.values method returns the enumerable values of given object as


an array. See the behaviour of this method below.

Object.values({name: 'Amar', email: 'xyz@gmail.com', phone:


'9848986525'}); // ['Amar', 'xyz@gmail.com', '9848986525']

Object.values([2, 3, 19, 'Computers']). // [2, 3, 19, "Computers"]

Object.values({name: 'Amar', money: [200, 300, '$']}) // ['Amar',


[200, 300, '$']]

Object.values(NaN); // []

Object.values(null); // Throws an error

Object.values(undefined) //Throws an error

// Uncaught TypeError: Cannot convert undefined or null to object

Object.values(function() { }). // []

Object.entries

This method returns an enumerable array of key and value pairs like [key,

value] ` same as for...in ` loop.

The behaviour of Object.entries is

const obj = {'name': 'Amar', 'email': 'amar@gmail.com'};

Example 1:
console.log(Object.entries(obj));
Output: [['name', 'Amar'], ['email', 'amar@gmail.com']]

Example 2:
for(const [key, value] of Object.entries(obj)){
console.log(`${key}: ${value}`);
}
Output:
name: Amar
email: amar@gmail.com

Example:3
const obj1 = {id:1, name: 'Amar', prices: [23,24,25, '$']};
for(const [key, value] of Object.entries(obj1)) {
console.log(`${key}: ${value}`)
}

Output:
id: 1
name: Amar
prices: 23,24,25,$

String.prototype.padStart

This method allows to padding at the starting of the string multiple times
until it reaches to the given length

For instance

const str = '5';


console.log(str.padStart(5, 0)); // '00005'

const str = 5;
console.log(str.padStart(5, 0)); // Uncaught TypeError: str.padStart
is not a function

const str1 = null;


console.log(str1.padStart(5, 0)); // Uncaught TypeError: Cannot read
property 'padStart' of null

let num = NaN;


console.log(Number.padStart(5, 0)); // Uncaught TypeError:
Number.padStart is not a function

String.prototype.padEnd

This method allows to add padding at the end of the string until it reaches to
the given length. Let’s see the behaviour of this method

const str1 = '5';


console.log(str1.padEnd(5, 0)); // '50000'

Trailing commas in function parameter list

Trailing commas or final commas are functional while adding new elements
parameters and properties in Javascript. ECMA 2017 (ES8) allows trailing
commas to function parameter list and function calls. So, Let’s crack how to
use this.

function sum(a, b,) {


// Write some code here
}

(a,b,) => { } // Valid

Now, trailing commas with class methods

class Formulas {

sum(a,b,) {
return a+b;
}

multiply(a,b,) {
return a*b;
}
}

Usually, trailing commas used to add new property in a new line without
modifying the existing line. But, ES8 supports add trailing commas in
function parameters too. To know more trailing commas then you guys can
refer this link. You can go through plenty of examples given in the link to
understand the main use case of trailing commas in Javascript.

Async functions (async and await)


This feature is a game changer to deal with asynchronous functions and
methods. This method introduced a pretty new of handling promises. Here,
the behaviour of async and await functions.

function getAsyncContent() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Hi! How are you ?');
}, 3000);
});
}

async function getData(){


const result = await getAsyncContent();
console.log(result); //Hi! How are you ? (After three seconds)
}

//Here, the code without using async/await

function getData(){
getAsyncContent
.then(result => console.log(result))
.catch(error => console.log(error));
}

//IIFE(Immediately Invoked Function Expression) with async/await

(async () => {
const result = await getAsyncContent();
console.log(result); // Hi! How are you ? (After 3 seconds)
})();

Note: 1) We can use async/await to write unit test cases by using


MochaJs, JEST etc.
NodeJs compatibility

Async/Await is a striking change to the Javascript world as it avoid promise


callback hell completely.

Object.getOwnPropertyDescriptors

This method returns property descriptors of own properties.


For instance

const object1 = {
property1: 42
};

const descriptors1 = Object.getOwnPropertyDescriptors(object1);


console.log(descriptors1);
//Output: {property1: {writable: true, readable: true, value: 42,
enumerable}}

Note: If the writable is false then we can not replace the value of
that key in the object

In case, the purpose of writable, readable and enumerable are not known then
please update in comments then we can publish a separate article for this.

NodeJs Compatibility

All ES8 features started supporting from version 8.2.1. To be more specific
feature support in NodeJs see below.
Object.values — v7.5.0
Object.entries — v7.5.0
String.prototype.padStart — v8.2.1
String.prototype.padEnd — v8.2.1
Trailing commas in parameters — v8.2.1
Async functions — v7.10.1
Object.getOwnPropertyDescriptors— v7.5.0

As we shown these are main features that ECMA introduced in ES8 version.
This is one of the watermark version with its amazing features such as
async/await after ES6. Our main focus on this article is about ES8. That’s the
reason I could not cover much information about trailing commas use cases,
holes in arrays, iterator behaviour with holes in the array. Please mention in
comments if you are really interested to know deep details about trailing
commas then I will publish a separate article on this topic. Our next article on
ES9 will be published soon.

Get an email whenever Amar publishes.


Your email Subscribe

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about
our privacy practices.

Es8 Asyncawait Ecma2017 Padstart Es8 New Features

67 1

More from Amar Follow

Lead NodeJs developer and solution provider.

Jun 30

JavaScript New Features | ES7 (ECMA script


2016)
New Features
So, We all know that ES6 features very well. Most of the people are not able
to identify the features introduced in later version of ES6. If you ask ES6
features they are saying all features introduced ES7, ES8 and ES9 as well.

Let’s ask question yourself, is it necessary to differentiate features across


ECMA versions. If you ask this question to me then I would say Yes. The
reason is, there are libraries using Javascript they do not support all latest
ECMA latest features. Usually, they start support these features once a latest
version released. Normally, the latest version…

Read more · 2 min read

Share your ideas with millions of readers. Write on Medium

Dec 29, 2020

Run SequelizeJs migration and seeders with


NodeJs script.
Many of us using SequelizeJs ORM for RDBMS Database such as
PostgreSQL, MySQL and MSSQL. We can SequelizeJs for SQLite Database
for Desktop applications as well. For instance, development of desktop
applications by using ElectronJs.
The most common problem with SequelizeJs is running migration and
seeders for before going to deploy or install our application. Especially, if you
are using this for desktop applications where you need to run all these things
before starting the application for the first time. May be you have run this
when you get some updates also. …

Read more · 2 min read

More From Medium

The Right Way To Intro to using the State Simplicity is not the Solving Duplicate
Develop Your Ethereum Hook in React solution to Software Counting
Dapp is Here: DRIZZLE! Caitlin R
Complexity Jr Medina
Niharika Singh in Neeraj Sangal
HackerNoon.com

Javascript Building Mithril.js Analyze Overloading Promise to Learn


Blocks DAN
Function in TypeScrip Promises in JavaScript
Hamin Eduardo Tordecilla Paez Sachin Sarawgi

About Write Help Legal

You might also like