Latest Features introduced in ES7 and ES8
Posted By : Rudhishthir Prakash | 30-Aug-2019
Latest Features of ES7 and ES8
1. String.prototype.padStart/padEnd
The padStart method passes the current string with another string multiple times of needle until the resulting string reaches the given length. The padding is applied from start. Everything is same for the padEnd but the padding is applied from the end or from the right of the current string.
The syntax for the same is -
padStart - (desiredLength, textToAdd)
padEnd - (desiredLength, textToAdd)
Example -
const string = ''; console.log(string.padStart(10, 'Hi')); //Output -> HiHiHiHiHi console.log(string.padStart(10, 'JavaScript World')); //Output -> JavaScript console.log(string.padEnd(10, 'Hi')); //Output -> HiHiHiHiHi console.log(string.padEnd(10, 'JavaScript World')); //Output -> ript World
One of the usages of padStart could include prepending area code to phone number if the user input isn't the correct length. padEnd could be used for decimal precision.
2. Object.values
This method provides us with an array of the values of the objects' properties.
const obj = { name: 'John', age: '25', favoriteBooks: ['Harry Potter 1', 'Game of Thrones'] }; console.log(Object.values(obj)); //Output -> ['John', '25', ['Harry Potter 1', 'Game of Thrones']]
3. Object.entries
This function provides us with the array of the key-value pairs stored in arrays.
For example:
const obj = { name: 'John', age: '25', favoriteBooks: ['Harry Potter 1', 'Game of Thrones'] }; console.log(Object.entries(obj)); /*Output -> [ ['name', 'john'], ['age', 20], ['favoriteBooks', ['Harry Potter 1', 'Game of Thrones']] ] */
4. Exponentiation
ES8 has provided us with a new way of dealing with powers of a number. Earlier we had to use Math.pow() in order to do the exponentiation, but now we can simply use ** for it.
Example - console.log(2 ** 3); //Output -> 8
5. Trailing Commas-
These are not a new feature, instead, they are a fix for something that we can write wrong. They are the ability of a compiler not to raise a syntax error when we add an unnecessary comma at the end of the list/object.
For example:
For example: const obj = { name: 'John', age: '25', favoriteBooks: ['Harry Potter 1', 'Game of Thrones'], //This would not raise a compiler error };
These are some simple but useful features that can be incorporated into our everyday programming.
Request for Proposal
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Rudhishthir Prakash
Rudhishthir is a technical enthusiast having experience in C#.NET, NodeJS & various front-end technologies. He has great experience in building quality applications with innovative ideas. He also has proven expertise in handling clients.