Skip to content

Home

Assign default values for a JavaScript object's properties

If you have worked with classes for any length of time, you have probably encountered the need to assign default values to object properties. This is usually handled by the constructor function, but what if you want to assign default values to an object that is not a class instance?

Luckily, we can use Object.assign() for this task. Using this method, we can create a new empty object and copy the original object to maintain the key order. We can then use the spread operator (...) and Array.prototype.reverse() to combine the default values from left to right. If multiple default values are provided for the same property, the first one will take precedence.

Finally, we can use the original object again to overwrite properties that originally had a value. This way, we can assign default values to all properties in an object that are undefined.

const defaults = (obj, ...defs) =>
  Object.assign({}, obj, ...defs.reverse(), obj);

defaults({ a: 1 }, { b: 2 }, { b: 6 }, { a: 3 }); // { a: 1, b: 2 }
šŸ’” Tip

If you find yourself needing to assign the same default values to multiple objects, you can create a factory function that generates a new function with the default values already set. This way, you can reuse the same defaults across different objects and optimize your code.

More like this

Start typing a keyphrase to see matching snippets.