Logical AND assignment (&&=)
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.
Thelogical AND assignment (&&=
) operator only evaluates the right operand and assigns to the left if the left operand istruthy.
Try it
let a = 1;let b = 0;a &&= 2;console.log(a);// Expected output: 2b &&= 2;console.log(b);// Expected output: 0
Syntax
js
x &&= y
Description
Logical AND assignmentshort-circuits, meaning thatx &&= y
is equivalent tox && (x = y)
, except that the expressionx
is only evaluated once.
No assignment is performed if the left-hand side is not truthy, due to short-circuiting of thelogical AND operator. For example, the following does not throw an error, despitex
beingconst
:
js
const x = 0;x &&= 2;
Neither would the following trigger the setter:
js
const x = { get value() { return 0; }, set value(v) { console.log("Setter called"); },};x.value &&= 2;
In fact, ifx
is not truthy,y
is not evaluated at all.
js
const x = 0;x &&= console.log("y evaluated");// Logs nothing
Examples
Using logical AND assignment
js
let x = 0;let y = 1;x &&= 0; // 0x &&= 1; // 0y &&= 1; // 1y &&= 0; // 0
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-assignment-operators |