Default Values For Object Properties in JavaScript
Default values can be used now in all modern JS environments like so:
function greet(name = '') { return 'Hello ' + name; } console.log(greet('Phil')); // Hello Phil console.log(greet()); // Hello
Though what is not often known is that these do not have to be primitive values like strings, they can be objects to. So default property settings are also useful for large objects. Which is useful for complex functions which take objects as configuration:
function createMessage(config = { type: 'default', name: 'Person', date: new Date(), }) { let message = ''; if (config.type === 'default') { message = `Hello, ${config.name}. You have received a package on ${config.date}.`; } else if (config.type === 'warning') { message = `${config.name}, a package has been in storage since ${config.date}, `; message += 'and will be sent back to sender in 1 day.' } return message; } console.log(createMessage()); // Hello, Person. You have received a package on <NOW>. console.log(createMessage({ type: 'warning', name: 'Phil', date: new Date('2019-01-27T08:00:00'), })); // Phil, a package has been in storage since Sun Jan 27 2019 08:00:00 GMT-0500 (EST), // and will be returned in 1 day.
Github Location: https://github.com/Jacob-Friesen/obscurejs/blob/master/2019/defaultValuesObjectParams.js















