Did You Know This JavaScript Trick? (Logical Assignment)

preview_player
Показать описание
In today's video we'll be taking a look at Logical Assignment in JavaScript which lets you re-assign a variable depending on if it's truthy, falsy or nullish.

This is particularly useful when trying to set default values and avoids having to use a IF statement.

For your reference, check this out:

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

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

much more informative and easier to understand than the udeny course I paid for

herozuki
Автор

but the code gets much harder to read and if somebody dont now this they get stuck when they maintaining your code, so use it with care....

ore_bear
Автор

very very informative Dom! Keep it up🔥

ei_acc
Автор

Thanks for the explanation! I got it now

BakuraRyuk
Автор

a &&= 20 is not equals if (a) { a = 20}

a &&= 20 is equals to a = a && 20
a ||= 20 is equals to a = a || 20
a ??= 20 is equals to a = a ?? 20

Both are one-line expressions. The only problem is understanding the new operators.

I have a feeling JavaScript will soon become similar with this abbreviation

a ??= false && 240 ? true : 0 ? undefined : null

elementgames
Автор

Yes it's AND and OR this logical gates are never forget to me.

yerenzter
Автор

This is called short-circuiting too, right ?

abiodun
Автор

This is why people hate JS.
But a useful content!

Dimich
Автор

JS WHY SO SERIOUS :D
const tester = 12
let foo
foo = tester === 12 && ('YES') || ('NO')
console.log(foo) // YES

isuke
visit shbcf.ru