Add nullish coalescing assignment operator to Luau

As a Roblox developer, it is currently too hard to assign a default value to a variable only when it’s nil without writing it out the long way every time.

Right now if I want to set a value only when something is nil, I have to write:

config.Volume = config.Volume == nil and 1 or config.Volume

or

if config.Volume == nil then
    config.Volume = 1
end

This comes up constantly when working with config tables, player data, optional function arguments, and caching patterns where I only want to compute or assign something if it hasn’t already been set. Other modern languages handle this with a nullish coalescing assignment operator (??=), which only assigns when the left side is nil. Luau has no equivalent, so I end up writing repetitive boilerplate or relying on the and/or trick, which is error prone with falsy-but-not-nil values like false.

The and/or pattern silently overwrites a legitimate false value because it checks for truthiness rather than nil. That means boolean config flags and similar patterns can’t safely use it at all.

If Roblox is able to address this issue, it would improve my development experience because I could write something like:

config.Volume ??= 1

instead of the longer patterns above. This would cut down on repeated boilerplate, make intent clearer (only assign if nil), and avoid the and/or bug where a legitimate false gets overwritten. The operator would be consistent with Luau’s existing compound assignment operators (+=, -=, etc.) and would not require any new semantics beyond nil-checking on assignment.

13 Likes

This isn’t documented on the operators page, what does this do?

2 Likes

This is the Feature Requests category…

Now regarding the original post, I really like this idea and think it should be added, either ??= or ?= would be a solid and easy to implement feature

2 Likes

Ooh I love this idea. Yes please add this..

2 Likes

They were asking OP to elaborate on their claim that an operator which was not documented supposedly exists in Luau. Surely reading comprehension isn’t that hard?

2 Likes

That’s exactly how I interpreted the comment too… That’s why I pointed out it was the Feature Requests category? And you’re the one telling me reading comprehension isn’t hard…

1 Like

I hope you realize that, this category being for requests, users need to support them. And users need primarily to know what they’re supporting. This operator isn’t documented in the Creator Docs, so a question “what is it?” fits perfectly for future readers.
This is getting off-topic and I still haven’t gotten an answer so, I’d appreciate at this point just continuing to use the like button.

2 Likes

It’s perfectly clear from the post what the operator would do. Instead of typing

if config.Volume == nil then
    config.Volume = 1
end

you can just type

config.Volume ??= 1

instead. How much clearer could it possibly be? Why would you expect a non-existent operator to be documented on the operators page?

2 Likes

Because prior to OP editing it out of his post, he was implying that the operator ?? (NOT ??=) actually existed in either standard Lua or Luau and that is what was being asked about.

3 Likes

alr bro sorry, basically this category is for requests and the ?? operator doesn’t exist, that’s what I meant when I said it’s the feature requests category, cause the operator is requested and doesn’t exist yet

You have to make a request at https://rfcs.luau.org/ in order to get stuff like this passed. Also take a look at the other cool request!

2 Likes

Yeah, this was my bad. I corrected it pretty quickly but looks like it had some knock on effects lol.

Thank you for the information, I’ve opened a PR.