ES2021 Logical Assignment Operator 🧵
We all have heard and read about the logical Operator but JavaScript in the 2021 version has a new operator called Logical Assignment Operator.
→ 3 new operators are:
Logical OR assignment operator ( ||= )
Logical AND assignment operator ( &&=)
Nullish Coalescing assignment operator (??=)
→ Logical OR assignment operator ( ||= )
Syntax: x ||= y, another way to represent x || (x = y)
Explain: If x is falsy then value of x will be y
Example: let myName;
myName ||= "Vansh"
Explain:
If myName is falsy then myName will be assigned value "Vansh".
Here myName is falsy therefore
myName = "Vansh"
→ Logical AND Assignment Operator ( &&= )
Syntax: x &&= y, another way to represent x && (x = y)
Explain: If x is truthy then value of x will be y
Example: let lastName = "Sharma";
lastName &&= "Bhardwaj"
Explain:
If lastName is truthy then lastName will be assigned the value "Bhardwaj".
Here lastName is truthy therefore
lastName = "Bhardwaj"
→ Nullish Coalescing assignment operator (??=)
Syntax: x ??= y, another way to represent x ?? (x = y)
Explain: If x is null and undefined then the value of x will be y
Example : let city=null;
city ??= 'Delhi';
Explain: Here city is null therefore nullish coalescing operator will assign Delhi to the city.