I have seen this syntax in node.jsv15.0.1: &&=
, ||=
and ??=
.
But I don't know what it does. Does anyone know?
Purpose can guide life decisions, influence behavior, shape goals, offer a sense of direction, and create meaning. For some people, purpose is connected to vocation—meaningful, satisfying work. For others, their purpose lies in their responsibilities to their family or friends.
When people say “on purpose”, they generally mean deliberately. Or not by accident.
Purpose is defined as to plan or intend to do something. An example of purpose is someone deciding they will start saving 10% of their income. verb. 4. An object to be reached; a target; an aim; a goal.
the reason for which something exists or is done, made, used, etc. an intended or desired result; end; aim; goal. determination; resoluteness.
These are the new logical assignment operators. They're similar to the more familiar operators like *=
, +=
, etc.
someVar &&= someExpression
is roughly equivalent to someVar = someVar && someExpression
.
someVar ||= someExpression
is roughly equivalent to someVar = someVar || someExpression
.
someVar ??= someExpression
is roughly equivalent to someVar = someVar ?? someExpression
.
I say "roughly" because there's one difference - if the expression on the right-hand side isn't used, possible setters are not invoked. So it's a bit closer to:
someVar &&= someExpression
is like
if (!someVar) {
someVar = someExpression;
}
and so on. (The fact that a setter isn't invoked is unlikely to have an effect on the script, but it's not impossible.) This is unlike the other traditional shorthand assignment operators which do unconditionally assign to the variable or property (and thus invoke setters). Here's a snippet to demonstrate:
const obj = {
_prop: 1,
set prop(newVal) {
this._prop = newVal;
},
get prop() {
return this._prop;
}
};
// Setter does not get invoked:
obj.prop ||= 5;
??
, if you aren't familiar with it, is the nullish coalescing operator. It will evaluate to the right-hand side if the left-hand side is either null
or undefined
.
Those are called Logical Assignment Operators and there are three in total:
&&=
)||=
)??=
)Fundamentally they all do the same: The logical operators &&
, ??
and ||
in front of the =
as in x logical-operator= y
can be rewritten as x logical-operator (x = y)
. Their only purpose is to replace more verbose code:
x &&= y
returns x when x is not truthy and returns y when x is truthy. It is the same as:
if (x) {
x = y
}
x ||= y
returns x when x is truthy and returns y when x is not truthy. It is the same as:
if (!x) {
x = y
}
x ??= y
returns x when x is not nullish and returns y when x is nullish. It is the same as:
if (x === null || x === undefined) {
x = y
}
Here are some examples to further your understanding of these:
const y = 'other value'
let def = 'initial' // truthy value
let zero = 0 // not truth value
let undef = undefined // nullish value
def &&= y // def = 'other value'
zero &&= y // zero = 0
undef &&= y // undef = 'undefined'
def ||= y // def = 'initial'
zero ||= y // zero = 'other value'
undef ||= y // undef = 'other value'
def ??= y // def = 'initial'
zero ??= y // zero = 0
undef ??= y // undef = 'other value'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With