Logical AND assignment (&&=)

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2020.

The logical AND assignment (&&=) operator only evaluates the right operand and assigns to the left if the left operand is truthy.

Try it

let a = 1;
let b = 0;

a &&= 2;
console.log(a);
// Expected output: 2

b &&= 2;
console.log(b);
// Expected output: 0

Syntax

js
x &&= y

Description

Logical AND assignment short-circuits, meaning that x &&= y is equivalent to x && (x = y), except that the expression x is only evaluated once.

No assignment is performed if the left-hand side is not truthy, due to short-circuiting of the logical AND operator. For example, the following does not throw an error, despite x being const:

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, if x 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; // 0
x &&= 1; // 0
y &&= 1; // 1
y &&= 0; // 0

Specifications

Specification
ECMAScript® 2027 Language Specification
# sec-assignment-operators

Browser compatibility

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Bun
Deno
Node.js
Logical AND assignment (x &&= y)
Chrome – Full support
Chrome 85 (Release date: 2020-08-25)
footnote Full support
Edge – Full support
Edge 85 (Release date: 2020-08-27)
footnote Full support
Firefox – Full support
Firefox 79 (Release date: 2020-07-28)
footnote Full support
Opera – Full support
Opera 71 (Release date: 2020-09-15)
footnote Full support
Safari – Full support
Safari 14 (Release date: 2020-09-16)
footnote Full support
Chrome Android – Full support
Chrome Android 85 (Release date: 2020-08-25)
footnote Full support
Firefox for Android – Full support
Firefox for Android 79 (Release date: 2020-07-28)
footnote Full support
Opera Android – Full support
Opera Android 60 (Release date: 2020-09-23)
footnote Full support
Safari on iOS – Full support
Safari on iOS 14 (Release date: 2020-09-16)
footnote Full support
Samsung Internet – Full support
Samsung Internet 14 (Release date: 2021-04-17)
footnote Full support
WebView Android – Full support
WebView Android 85 (Release date: 2020-08-25)
footnote Full support
WebView on iOS – Full support
WebView on iOS 14 (Release date: 2020-09-16)
footnote Full support
Bun – Full support
Bun 1 (Release date: 2023-09-08)
footnote Full support
Deno – Full support
Deno 1.2 (Release date: 2020-07-13)
footnote Full support
Node.js – Full support
Node.js 15 (Release date: 2020-10-20)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

See also