JavaScript is notorious for being "loose", something that some developers love but other developers loathe. I hear most of those complaints from server side developers, who want string typing and syntax. While I like strict coding standards, I also like that JavaScript lets me quickly prototype without having to cross the I's and dot the T's. Until recently you couldn't define default parameter values for functions in JavaScript, but now you can!
When I posted last week about Six Tiny but Awesome ES6 Features, an awesome reader (cmwd) pointed out that you can not only set default function parameter values but you can throw errors when a given parameter isn't provided to a function:
const isRequired = () => { throw new Error('param is required'); };
const hello = (name = isRequired()) => { console.log(`hello ${name}`) };
// This will throw an error because no name is provided
hello();
// This will also throw an error
hello(undefined);
// These are good!
hello(null);
hello('David');
I love this tip -- it shows how with each addition to JavaScript we can stretch the language to do interesting things. How practical it is to throw errors in production is up to you but this is an awesome ability during development. Happy coding!


