Skip to content

Suggestion: support ||= compound assignment #20378

Closed
@Xample

Description

@Xample

Perhaps there is a good reason for this but I never understood why the compound assignment ||= is not part of js. This would be helpful to ensure a variable to be defined.

Given:

let a = {"key":"value"}

This would let us write:

a.key ||= "default";
a.anotherKey ||= "anotherDefault";

Instead of

a.key = a.key || "default";
a.anotherKey = a.anotherKey || "anotherDefault";

Of course we could have used

a = {...a, key:"default", anotherKey:"anotherDefault"}

But as a general case, this should work on any variable

let a;
a ||= "default";

instead of

let a;
a = a ||"default";

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions