đ¨ All you need to know before using modern JavaScript features
Dawid Zajac
Posted on March 13, 2023
The JavaScript language is constantly evolving. I bet youâve heard of ES6 which is so far the most popular version of this language. It was released back in 2015 and since then, JavaScript has been receiving a new version every year.
Each version introduced some new functions or syntax (or both). An example might be ES12 which have introduced:
-
String.prototype.replaceAll
function - Separators for numeric literals e.g.
10_000
(easier to read equivalent of10000
)
and some more features which I wonât mention in this article.
Does it mean you can start using new features immediately? The answer is yes, thatâs what weâre getting them for! But⌠you might need to use some polyfills and a transpiler.
Why do we need polyfills and transpilers?
JavaScript may run in different environments. Those environments evolve simultaneously with JavaScript, but their previous versions donât support new syntax nor functions. For example, a script using mentioned before String.prototype.replaceAll function will work fine if run on NodeJS v15+ but will fail to run on NodeJS versions prior to v15. Why is that? Because NodeJS v15 have added support for ES12. To make a script work on older versions of NodeJS, we need to use a polyfill function.
Note: We could update our NodeJS version to v15+ instead, but thatâs something usually harder to do than adding a polyfill, especially if our app have a lot of dependencies.
Polyfills
Polyfilling is a concept of implementing a fallback function if the environment running our code doesnât have one implemented yet. Polyfills make it easier to write code that will run the same way on different environments. Polyfill functions should be implemented on top of your app, before any app related code so that missing functions have been polyfilled before executing your appâs code.
A polyfill function for String.prototype.replaceAll
would look like:
// If statement checks if we need a polyfill
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function(str, newStr){
// If a regex pattern
if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
return this.replace(str, newStr);
}
// If a string
return this.replace(new RegExp(str, 'g'), newStr);
};
}
/**
* Author: Chris Ferdinandi
*/
With the polyfill implemented, weâre safe to run our code on older (than v15) versions of NodeJS and it wonât throw errors any more! đ
Usually you donât have to write a polyfill funtion on your own. Thereâs a high chance that somebody has already released a polyfill youâre looking for. Google your feature name + âpolyfillâ and youâll most likely find something. Some polyfills are also available on NPM.
Note: Do not polyfill your code âjust in caseâ. If youâre sure, that target environments do support the feature youâre about to polyfillâââyouâre safe, no polyfill needed!
Transpilers
Transpiler is a software which translate source code into another source code. It looks for usages of modern syntax and replace it with equivalent code written using older syntax which is widely supported, so that your code will run on more environments seamlessly.
Letâs take a look at the example of code using syntax called ânullish coalescing operatorâ ( ??
) which has been introduced by ES11.
// Original source code
const errorMessage = errorMessage ?? 'Default error message';
// Transpiled source code
const errorMessage =
(errorMessage !== undefined && errorMessage !== null)
? errorMessage
: 'Default error message'
Transpiler rewrote our modern code using older syntax which is now understandable by environments not supporting ES11 features yet.
In JavaScript world, the most popular transpiler is Babel. Visit their website and play with their online code editor and itâs different settings to find out what your source code gets transpiled to.
Transpilers are most often integrated with bundlers like Webpack, Rollup etc. which transpile the code âunder the hoodâ e.g. during the build process.
Tools
There are several tools that allow you to check what features are supported by which environment. Worth knowing are:
caniuse.com â Browser oriented. Useful especially for Front-end developers. Supports JS & CSS features.
node.green â Node.js oriented. Useful for Back-end developers.
Summary
Polyfills and transpilers let you write modern code and run it on older environments.
- Polyfills are used to fill the gap of missing functions
- Transpilers are used to replace modern syntax with equivalent older syntax
Hope youâve enjoyed and learned something new. Thank you for reading!
Posted on March 13, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.