ECMAScript 2015 - everyone still calls it ES6 - shipped over a year ago, but 2017 is when it's become the default rather than the exception on our projects, partly thanks to Babel filling in the gaps where native support is still catching up. A few features have changed daily habits enough that it's worth walking through them directly.
let and const replace var
var is function-scoped, which produces surprising bugs inside loops and conditionals. let and const are block-scoped, which matches how most other languages behave and how developers already expect scope to work:
for (let i = 0; i < items.length; i++) {
setTimeout(() => console.log(i), 100); // logs 0,1,2 - not 3,3,3
}
Our rule of thumb: default to const, switch to let only when a value genuinely needs to be reassigned, and treat a lingering var in a code review as a question worth asking.
Arrow functions and lexical this
class Carousel {
constructor(el) {
this.el = el;
el.addEventListener('click', () => this.next()); // `this` is preserved
}
}
Arrow functions don't rebind this, which quietly removes an entire category of "why is this undefined inside my callback" bugs that used to require .bind(this) scattered through constructors.
Template literals and destructuring
const name = 'Pranta Tech';
const msg = `Welcome to ${name}, est. 2015`;
const { title, price } = product;
const [first, ...rest] = items;
Template literals end the era of 'Welcome to ' + name + ', est. ' + year. Destructuring pulls values out of objects and arrays directly, which pairs especially well with function parameters:
function renderCard({ title, price, image }) { /* ... */ }
Modules, properly
// utils.js
export function formatPrice(cents) { return (cents / 100).toFixed(2); }
// app.js
import { formatPrice } from './utils.js';
Native import/export finally gives JavaScript a real module system, instead of the patchwork of IIFEs and global namespacing it managed with before.
Classes: syntax, not a new object model
ES6 classes are sugar over JavaScript's existing prototype chain - nothing about the underlying object model changed - but the syntax reads far more clearly to anyone coming from another language, and that clarity is worth adopting on its own.
The features that changed how JavaScript actually gets written
const { name, age } = user; // destructuring
const greet = (name) => `Hi, ${name}`; // arrow fn + template literal
const merged = { ...defaults, ...overrides }; // spread
Destructuring and the spread operator alone eliminated a huge amount of repetitive property-access and object-merging boilerplate that used to be standard in pre-ES6 codebases.
Promises deserve special mention
Before native Promises, asynchronous code meant nested callbacks several levels deep - "callback hell" wasn't an exaggeration, it was a genuine maintainability crisis in large codebases. Promises, and later async/await built on top of them, turned asynchronous logic back into code that reads top-to-bottom like synchronous code:
async function loadUser(id) {
const res = await fetch(`/api/users/${id}`);
return res.json();
}