JavaScript tips — The logical or assignment operator ||=

Описание к видео JavaScript tips — The logical or assignment operator ||=

The logical or assignment operator in #JavaScript be convenient shorthand for logical expressions

It also is a short way to set default values. If you write obj.title ||= ""new string"", the obj.title property will only be changed if it has not already been set or is the empty string

Unlike other assignment operators, the logical or assignment operator also short circuits. Right right handsome will not be evaluated if the left hand side is already true

Note that I've intentionally simplified the expanded form of the assignment expression. Something like myVar ||= true actually expands to myVar || (myVar || true). This difference usually doesn't matter, but it means that the short circuiting of the logical or assignment can actually skip assigning values

https://developer.mozilla.org/en-US/d...

Комментарии

Информация по комментариям в разработке