CKTIS / JavaScript
Logical AND assignment (&&=)
The &&= operator evaluates the right-hand side and assigns its value only when the target's current value is truthy. Otherwise, it skips both the right-hand side and the write.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
target &&= expressiontarget &&= expression reads the target's value. If it is falsy, it returns that value without evaluating expression or assigning anything. Otherwise, it evaluates expression, assigns the result to the target, and returns that result. It does not convert the stored value to Boolean.
The target is evaluated only once. Thus, target &&= expression is not generally equivalent to target = target && expression: the latter always attempts assignment. This matters for side effects and setters. A getter may run to read the value, but a falsy value prevents the setter from running.
This is not a default-value operator: ||= assigns for a falsy value, whereas ??= assigns only for null or undefined. The target must be a valid assignment target. const does not prevent changing an object's properties, but writing to the const binding itself, if attempted, throws TypeError.
In the example, state.active is truthy, so it receives "updated". state.empty is falsy, so it remains an empty string. The function replacement() is called only once.
Examples
let calls = 0;
function replacement() {
calls += 1;
return "updated";
}
const state = { active: "ready", empty: "" };
const first = (state.active &&= replacement());
const second = (state.empty &&= replacement());
console.log(JSON.stringify({ first, second, calls, state }));Result
{"first":"updated","second":"","calls":1,"state":{"active":"updated","empty":""}}The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.