4 Javascript ES6 Tricks
Chris Burgin
432

Another awesome checklist! One really neat thing about #2 (default function params) is that you can actually use them to enforce required values as well! Check it:

const required = (name='var') => {throw Error(`${name} required`)}
// all your "required" params can just point to the above function by default:
const sayHello = (fName="Chris", lName=required('lName')) => 
`Whats up ${fName} ${lName}`
console.log(sayHello("Tim", "Burgin")) // What is up Tim Burgin
console.log(sayHello("Tim")) // Error: lName required

With the error throwing stuff in place, you can do stuff such as:

try {
sayHello("Tim") // this will error!
} catch(e) {
console.log('Whoops'); // generally, handle the error here
}

or,

Promise.resolve()
.then(_ => sayHello("tim", "foo"))
.then(msg => console.log(msg))
.catch(e => console.log('nope'))
// ^^^ same as above but within promise chain

gg