This is the editorial from our latest JavaScript newsletter, you can subscribe here.
When was the last time you wrote some ES6 code? A recent developer survey called The State of JavaScript reported that 74% of respondents have already used the new syntax and intend to do so again. A further 21% have heard of it and are interested in learning. The ES2015 version of JavaScript (referred to as ES6 from here on) has been an official standard for almost a year and a half now, but it still seems like a lot of blog posts, articles, and tutorials are still being published that use the older ES5 syntax.
This past week, I spent some time working on a Chrome extension. One of the nice things about developing Chrome extensions is that the browser is fixed, and so you know what features are supported. The decision was made that the extension would be written in ES6-flavored JavaScript, which worked out well for me as I’ve recently been trying to utilize ES6 in my code whenever possible, in order to become fluent with the new syntax.
Of course, it’s not just Chrome that can run ES6 natively. Support for the new standard has come on leaps and bounds, with the latest releases of the major desktop browsers all complying with over 90% of the specification (with tail call optimization being the last major outstanding feature). The current releases of both Node 6 and 7 have similarly good levels of support. Of course, most of the time we don’t have the luxury of just coding for compliant browsers – we have to support mobile browsers (which still lag being in their support for ES6) and, naturally, older versions of IE.
Thankfully, we don’t have to give up on using modern JavaScript to support legacy engines thanks to the ability to transpile (convert the source code) to ES5-compatible code. This is sometimes mentioned as a reason against adopting ES6 at the current time, but I don’t find that a convincing argument. For a start, most real-world applications are going to be using a build step of some sort already; compiling SASS/Less, bundling modules, or, at the least, concatenating files. More significantly, it looks like transpiling will be with us for the foreseeable future. Even when the majority of users’ browsers support ES6 natively, we’ll most likely still be transpiling in order to use the latest and greatest version of the standard (ES2020, anyone?).
In my opinion, switching to ES6 is worthwhile for the syntax changes alone. You may be shaking your head, but I think things such as arrow functions, object property and method shorthands, default parameters, and template strings can really help to streamline your code. “These things are just syntactic sugar,” you might say. Taking default function parameters as an example, it might only take an extra line of code to do this in ES5 but every extra line you don’t have to type makes your code more concise and more readable. You can focus on logic over boilerplate.
It goes without saying that not everyone has been thrilled with the updates to the language. Probably the most contentious issue was the introduction of the new Class syntax, which attempts to hide JavaScript’s prototypal nature behind a facade that makes it appear like more traditional object-oriented languages. Critics argue that this obscures important details of how JavaScript’s prototype inheritance actually works, while some go as far as to advocate avoiding the use of classes altogether in favor of more idiomatic techniques, such as factory functions. Personally, I think that whichever side you happen to agree with it’s not an argument to avoid the other benefits of adopting ES6 – take or leave classes as you see fit.
So, when was the last time you wrote some ES6 code? Maybe you already use it on a day-to-day basis, or perhaps you hate the new syntax and have sworn to avoid it at all costs? Should we drop ES5 here at SitePoint and start exclusively publishing content in the modern syntax? I’d love to hear your thoughts, so please leave a comment or tweet me your thoughts @sitepointjs
Nilson is a full-stack web developer who has been working with computers and the web for over a decade. A former hardware technician, and network administrator. Nilson is now currently co-founder and developer of a company developing web applications for the construction industry. You can also find Nilson on the SitePoint Forums as a mentor.