Check out these useful ECMAScript 2015 (ES6) tips and tricks

8 min readMar 16, 2018
Photo by Glenn Carstens-Peters on Unsplash

EcmaScript 2015 (aka ES6) has been around for couple of years now, and various new features can be used in clever ways. I wanted to list and discuss some of them, since I think you’ll find them useful.

If you know of other tips, please let me know in the comment and I’ll be happy to add them.

1. Enforcing required parameters

ES6 provides default parameter values that allow you to set some default value to be used if the function is called without that parameter.

In the following example, we are setting the required() function as the default value for both a and b parameters. This means that if either a or b is not passed, the required() function is called and you’ll get an error.

const required = () => {throw new Error('Missing parameter')};//The below function will trow an error if either "a" or "b" is missing.
const add = (a = required(), b = required()) => a + b;
add(1, 2) //3
add(1) // Error: Missing parameter.

2. The mighty “reduce”

Array’s reduce method is extremely versatile. It is typically used to convert an array of items into a single value. But you can do a lot more with it.

🔥Tip: Most of these tricks rely on the initial value being an Array or an Object instead of a simple value like a string or a variable.

2.1 Using reduce to do both map and filter *simultaneously*

Suppose you have a situation where you have a list of items, and you want to update each item (that is, map) and then filter only a few items (that is, filter). But this means that you would need to run through the list twice!

In the below example, we want to double the value of items in the array and then pick only those that are greater than 50. Notice how we can use the powerful reduce method to both double (map) and then filter? That’s pretty efficient.


Trying to make Web development simple. Former Developer Advocate @Salesforce, VMware (node.js); Engineer @ Yahoo, Zimbra. Twitter: @rajaraodv