JavaScript ES2018 Version (2018)
JavaScript ES2018 (ECMAScript 2018) is the ninth major release of the JavaScript language specification. It was finalized in June 2018. ES2018 introduced several new features and enhancements to JavaScript, building upon the foundation laid by ES6 (ES2015). In this tutorial, we will learn about the new features introduced in ES2018 and how to use them in your JavaScript code.
Features of ES2018
ES2018 introduced several new features and enhancements to JavaScript. Some of the key features of ES2018 are:
-
Asynchronous Iteration: ES2018 introduced asynchronous iteration, which allows you to iterate over asynchronous data sources such as asynchronous generators and asynchronous iterators. You can use the
for-await-of
loop to iterate over asynchronous data sources.For example:
Asynchronous Iteration Exampleasync function* asyncGenerator() {
yield 1;
yield 2;
yield 3;
}
(async () => {
for await (let num of asyncGenerator()) {
console.log(num);
}
})(); -
Promise.prototype.finally(): ES2018 introduced the
Promise.prototype.finally()
method, which allows you to run cleanup logic after aPromise
is settled (either fulfilled or rejected). Thefinally()
method is called when thePromise
is settled, regardless of whether it was fulfilled or rejected.For example:
Promise.prototype.finally() Examplelet promise = new Promise((resolve, reject) => {
resolve("Success!");
});
promise.then((value) => {
console.log(value);
}).catch((error) => {
console.error(error);
}).finally(() => {
console.log("Cleanup logic here");
}); -
Rest/Spread Properties: ES2018 introduced the rest and spread properties for object literals. The rest properties allow you to extract the remaining properties of an object into a new object, while the spread properties allow you to spread the properties of an object into another object.
For example:
Rest/Spread Properties Examplelet { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // Output: 1
console.log(y); // Output: 2
console.log(z); // Output: { a: 3, b: 4 } -
Object Rest/Spread Properties: ES2018 also introduced rest and spread properties for object destructuring. The rest properties allow you to extract the remaining properties of an object into a new object, while the spread properties allow you to spread the properties of an object into another object.
For example:
Object Rest/Spread Properties Examplelet { x, y, ...z } = { x: 1, y: 2, a: 3, b: 4 };
console.log(x); // Output: 1
console.log(y); // Output: 2
console.log(z); // Output: { a: 3, b: 4 } -
RegExp Lookbehind Assertions: ES2018 introduced lookbehind assertions for regular expressions. Lookbehind assertions allow you to match a pattern only if it is preceded by another pattern.
For example:
RegExp Lookbehind Assertions Examplelet str = "Hello, World!";
let regex = /(?<=Hello, )World/;
console.log(regex.test(str)); // Output: true -
RegExp Unicode Property Escapes: ES2018 introduced Unicode property escapes for regular expressions. Unicode property escapes allow you to match characters based on their Unicode properties.
For example:
RegExp Unicode Property Escapes Examplelet str = "😀";
let regex = /\p{Emoji}/u;
console.log(regex.test(str)); // Output: true
Browser Support for ES2018
Most modern web browsers support ES2018 features, but some older browsers may not fully support all ES2018 features. To ensure that your JavaScript code works across all browsers, you can use a transpiler like Babel to convert your ES2018 code into ES5 code, which is compatible with older browsers.
ES2018 features are generally well-supported in modern web browsers, and you can start using them in your JavaScript code without the need for a transpiler in most cases.
For more information on browser support for ES2018 features, you can check the table.
66+ | 58+ | 11.1+ | 79+ | 53+ | No | |||||||
Latest ✅ | Latest ✅ | Latest ✅ | Latest ✅ | Latest ✅ | Latest ❌ |
-
Chrome: 66+ (Latest ✅)
Chrome has full support for ES2018 features. You can use ES2018 features in Chrome without any issues. Chrome 66 and later versions fully support ES2018 features. Chrome is the most popular web browser, and it is recommended to test your JavaScript code in Chrome to ensure compatibility.
-
Firefox: 58+ (Latest ✅)
Firefox has full support for ES2018 features. You can use ES2018 features in Firefox without any issues. Firefox 58 and later versions fully support ES2018 features. Firefox is a popular web browser, and it is recommended to test your JavaScript code in Firefox to ensure compatibility.
-
Safari: 11.1+ (Latest ✅)
Safari has full support for ES2018 features. You can use ES2018 features in Safari without any issues. Safari 11.1 and later versions fully support ES2018 features. Safari is a popular web browser, and it is recommended to test your JavaScript code in Safari to ensure compatibility.
-
Edge: 79+ (Latest ✅)
Edge has full support for ES2018 features. You can use ES2018 features in Edge without any issues. Edge 79 and later versions fully support ES2018 features. Edge is a popular web browser, and it is recommended to test your JavaScript code in Edge to ensure compatibility.
-
Opera: 53+ (Latest ✅)
Opera has full support for ES2018 features. You can use ES2018 features in Opera without any issues. Opera 53 and later versions fully support ES2018 features. Opera is a popular web browser, and it is recommended to test your JavaScript code in Opera to ensure compatibility.
-
Internet Explorer: No (Latest ❌)
Internet Explorer does not support ES2018 features. If you need to support Internet Explorer, you can use a transpiler like Babel to convert your ES2018 code into ES5 code, which is compatible with Internet Explorer.
ES2018 introduced several new features and enhancements to JavaScript, making it more powerful and expressive. You can start using ES2018 features in your JavaScript code to take advantage of these new capabilities and improve your development workflow.
In the next tutorial, we will learn about the ES2019 version of JavaScript and the new features introduced in ES2019.
Conclusion
ES2018 introduced several new features and enhancements to JavaScript, making it easier to write asynchronous code, work with objects, manipulate strings, and work with regular expressions. You can start using ES2018 features in your JavaScript code to take advantage of these new capabilities.