Here is a safer way to default variables in Javascript #shorts

preview_player
Показать описание
A quick short about the nullish coalescing operator. Use it isn't of using the || fallback when possible.

#shorts

------------

Рекомендации по теме
Комментарии
Автор

the reason why you don't use || is because it treats 0, empty string etc as false values and will assign the default parameter to the variable, sometimes you dont want to do that and that's when null coalescing (??) comes into play as it only treats null and undefined as false values

ananthdev
Автор

He really made a new line just for that single param

mikopiko
Автор

could also do user.skills ??= DEFAULT_SKILLS

donnyroufs
Автор

The YouTube algorithm can be so oddly specific. It REALLY wants to show me content about the nullish coalescing operator for some reason 😂

alexjones
Автор

Hi, have a question, you said the function takes an user object but that’s parameter and anything can come in so in this case you just assume that you know what user parameter will be?? Like data type etc.,

ylovers
Автор

by using || 0 will consider a falsy value too. this is why ?? came to adress this issue.

Pilosofia
Автор

When you make a video to showcase that one method is better than another you should also explain why.

damagee
Автор

I think it is not good practice to update outter variable

volodymyrlomaka
Автор

const DEFAULT_SKILLS = ["javascript"];

function createUser({ skills = DEFAULT_SKILLS } = {}) {
console.log({ ...arguments[0], skills });
}

sawilliams
Автор

Bro we can't see full code because your face comes in middle.Please keep it at empty spaces in the code editor.

Treklucifer