← #35 Conditional logging in the Console
#36

Enforce required parameters

Topics: JavaScript

In ES6, the default value of a parameter is evaluated if the parameter is missing. It allows us to enfore that a given parameter is required.

const required = () => {
throw new Error('Missing parameter');
};

const getAges = (yearOfBirth = required()) => (
new Date().getFullYear() - yearOfBirth
);

Calling getAges() without parameter will throw the exception Missing parameter.

Fix a typo or suggest an improvement
#37 Create an object with dynamic keys