The JavaScript language steadily evolves. New proposals to the language appear regularly, they are analyzed and, if considered worthy, are appended to the list at https://tc39.github.io/ecma262/ and then progress to the specification.
Teams behind JavaScript engines have their own ideas about what to implement first. They may decide to implement proposals that are in draft and postpone things that are already in the spec, because they are less interesting or just harder to do.
So itâs quite common for an engine to implement only part of the standard.
A good page to see the current state of support for language features is https://compat-table.github.io/compat-table/es6/ (itâs big, we have a lot to study yet).
As programmers, weâd like to use most recent features. The more good stuff â the better!
On the other hand, how to make our modern code work on older engines that donât understand recent features yet?
There are two tools for that:
- Transpilers.
- Polyfills.
Here, in this chapter, our purpose is to get the gist of how they work, and their place in web development.
Transpilers
A transpiler is a special piece of software that translates source code to another source code. It can parse (âread and understandâ) modern code and rewrite it using older syntax constructs, so that itâll also work in outdated engines.
E.g. JavaScript before year 2020 didnât have the ânullish coalescing operatorâ ??. So, if a visitor uses an outdated browser, it may fail to understand the code like height = height ?? 100.
A transpiler would analyze our code and rewrite height ?? 100 into (height !== undefined && height !== null) ? height : 100.
// before running the transpiler
height = height ?? 100;
// after running the transpiler
height = (height !== undefined && height !== null) ? height : 100;
Now the rewritten code is suitable for older JavaScript engines.
Usually, a developer runs the transpiler on their own computer, and then deploys the transpiled code to the server.
Speaking of names, Babel is one of the most prominent transpilers out there.
Modern project build systems, such as webpack, provide a means to run a transpiler automatically on every code change, so itâs very easy to integrate into the development process.
Polyfills
New language features may include not only syntax constructs and operators, but also built-in functions.
For example, Math.trunc(n) is a function that âcuts offâ the decimal part of a number, e.g Math.trunc(1.23) returns 1.
In some (very outdated) JavaScript engines, thereâs no Math.trunc, so such code will fail.
As weâre talking about new functions, not syntax changes, thereâs no need to transpile anything here. We just need to declare the missing function.
A script that updates/adds new functions is called âpolyfillâ. It âfills inâ the gap and adds missing implementations.
For this particular case, the polyfill for Math.trunc is a script that implements it, like this:
if (!Math.trunc) { // if no such function
// implement it
Math.trunc = function(number) {
// Math.ceil and Math.floor exist even in ancient JavaScript engines
// they are covered later in the tutorial
return number < 0 ? Math.ceil(number) : Math.floor(number);
};
}
JavaScript is a highly dynamic language. Scripts may add/modify any function, even built-in ones.
One interesting polyfill library is core-js, which supports a wide range of features and allows you to include only the ones you need.
Summary
In this chapter weâd like to motivate you to study modern and even âbleeding-edgeâ language features, even if they arenât yet well-supported by JavaScript engines.
Just donât forget to use a transpiler (if using modern syntax or operators) and polyfills (to add functions that may be missing). Theyâll ensure that the code works.
For example, later when youâre familiar with JavaScript, you can setup a code build system based on webpack with the babel-loader plugin.
Good resources that show the current state of support for various features:
- https://compat-table.github.io/compat-table/es6/ â for pure JavaScript.
- https://caniuse.com/ â for browser-related functions.
P.S. Google Chrome is usually the most up-to-date with language features, try it if a tutorial demo fails. Most tutorial demos work with any modern browser though.
Comments
<code>tag, for several lines â wrap them in<pre>tag, for more than 10 lines â use a sandbox (plnkr, jsbin, codepenâ¦)