`_.defaults` function on object using JavaScript Library underscore.js ( #shorts )

1 year ago
1

Hi Friends,

`_.defaults` function on object using JavaScript Library underscore.js

`_.defaults` is a utility function in Underscore.js library that ensure if an object has certain properties with default values, in case those properties are missing or undefined.

In below example, we have an object options that represents various options. We use _.defaults to set default values for the city and profession properties in case they are missing or undefined in the options object. After applying _.defaults, the options object is modified to include the default values for those properties.

Note: You can pass multiple default objects as arguments to _.defaults.

// syntax
_.defaults(object, *defaults)

// example
var options = {
name: "Alice",
age: 26
};

_.defaults(options, {
city: "New York",
profession: "Engineer"
});

console.log(options);
// { name: "Alice", age: 26, city: "New York", profession: "Engineer" }

#javascript #under #score #underscore #underscorejs #default #underscoredefaults #defaultsfunction #unique #object #array #string #nodejs #javascriptinterview #method #js #jquery #function #shorts #viral #popular #latest #new #coders #development #code #dev #interview #php #trend #trending #answer #laravel #youtube #instagram #socialmedia #education #edit #how #india #knowledge #quiz #reels #short #subscribe #share #update #video #web #webdeveloper #website #webdesign #ytshorts #youtuber #princecheema_official @princecheema_official

Loading comments...