Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?

JavaScript has ??, and is used by doing x ?? y.
The purpose of which is to replace null-like values with another, instead of ||, which favors the right hand side if the left is falsy, regardless of whether it is nullish or not.
To understand it better,…


This content originally appeared on DEV Community and was authored by DEV Community

JavaScript has ??, and is used by doing x ?? y.
The purpose of which is to replace null-like values with another, instead of ||, which favors the right hand side if the left is falsy, regardless of whether it is nullish or not.
To understand it better, read more about it on the Mozilla Developer Network, that can explain it better than me.

Anyways.
Where am I going with this?
... Well, if you look at the operators in JavaScript, you'll see a pattern with them:

a = a + b;    // Factorable to: a += b
a = a - b;    // Factorable to: a -= b
a = a ** b;   // You can even factor this to: a **= b

a = a ?? b;   // Can't be factored to: a ??= b
              // ...?

This isn't a big issue, but it would make the language more consistent.
Not only that, but it would be a nice way to clean up constructors. For example:

class Shape {
    constructor(x, y, w, h) {
        this.x = x ?? this.x;
        this.y = y ?? this.y;
        this.w = w ?? this.w;
        this.h = h ?? this.h;
    }

    // ...

    h = 0;
    w = 0;
    x = 0;
    y = 0;
}

// Could theoretically become...

class Shape {
    counstructor(x, y, w, h) {
        this.x ??= x;
        this.y ??= y;
        this.w ??= w;
        this.h ??= h;
    }
    // ...
}

As I said, it isn't a huge difference, but redundancy-removals similar to this are the reason += even exist in the first place.

So, I present to you the question posed in the title.
Why doesn't JavaScript have a nullish-coalescing assignment operator    (??=)?

Thanks for reading!
Cheers!


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-16T04:46:43+00:00) Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?. Retrieved from https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/

MLA
" » Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?." DEV Community | Sciencx - Wednesday March 16, 2022, https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/
HARVARD
DEV Community | Sciencx Wednesday March 16, 2022 » Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?., viewed ,<https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/>
VANCOUVER
DEV Community | Sciencx - » Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/
CHICAGO
" » Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/
IEEE
" » Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator?." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/. [Accessed: ]
rf:citation
» Why doesn’t JavaScript have a nullish-coalescing-assignment (??=) operator? | DEV Community | Sciencx | https://www.scien.cx/2022/03/16/why-doesnt-javascript-have-a-nullish-coalescing-assignment-operator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.