Answer would be Option 2
because while destructing of objects, default values would only be assigned when the key doesn't exist or value is undefined
. If the state
is the following
const state = {
user: {
id: undefined,
name: undefined,
subscribe: undefined,
link: undefined,
},
};
and now if we destructure
function getUser() {
const {
user: {
id = 1,
name = 'Devtools Tech',
subscribe = true,
link = 'https://bit.ly/devtools-yt',
} = {},
} = state;
return {
userId: id,
name,
subscribe,
link,
};
}
console.log(getUser());
Output would be:
{
"userId": 1,
"name": "Devtools Tech",
"subscribe": true,
"link": "https://bit.ly/devtools-yt"
}
Always remember, null
, false
, ''
, and 0
are all still values!
For more amazing tutorials, tips, and tricks: https://bit.ly/devtools-yt