FrontendDeveloper.in

ECMAScript question detail

Logical Operators

Logical assignment operators combines the logical operations(&&, || or ??) with assignment. They are quite useful for assigning default values to variables.

&&=:

The &&= operator performs the assignment only when the left operand is truthy.

let x = 10;
let y = 20;
x &&= y;
console.log(x); // 20

The above logical assignment operation can be expanded to:

x = x && (x = y);
(OR)
if (x) {
x = y;
}

||=:

The ||= operator performs the assignment only when the left operand is falsy.

let x = 0;
let y = 20;
x ||= y;
console.log(x); // 20

The above logical assignment operation can be expanded to:

x = x || (x = y);
(OR)
if (!x) {
x = y;
}

??=:

The ??= operator performs the assignment only when the left operand is null or undefined.

let x;
let y = 1;
x ??= y;
console.log(x); // 1

The above logical assignment operation can be expanded to:

x ?? (x = y);
(OR)
if (!x) {
x = y;
}

ES2022 Or ES13

ECMAScript 2022 or ES13 has been released in the month of June 2022 with some of important features in JavaScript.

Back to all ECMAScript questions
Get LinkedIn Premium at Rs 399