Posts

Showing posts from October, 2017

Clean code with ES6

You should always be as expressive as possible with your code. That usually means you will sometimes sacrifice wordiness with brevity. But sometimes you can get have the best of both worlds. Luckily, ES6 gives you a lot of tools to do just that. Object destructure func arguments Now we're getting a little fancy. But it's another great way to shorten code. const nestedSubarrays = [[1,2], [3,4]] for (let [i, j] of nestedSubarrays) { console.log(i); console.log(j) } Object destructuring can get really really fancy. const nestedSubarrays = [[1,2], [3,4], [5, 5], [6, 3]] const sortedBySecondEl = nestedSubarrays.sort(([a1, b1], [a2, b2]) => { return b1 - b2 }) Arrow functions can be one-liners The beauty of compact and verbose code. Note that one-liners are denoted using parenthesis, not curly braces. The parenthesis allow implicit return, as opposed to JavaScripts normal explicit return const arrowF