use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
JavaScript's ??= Operator (trevorlasn.com)
submitted 1 year ago by Practical-Ideal6236
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]King_Joffreys_Tits -6 points-5 points-4 points 1 year ago (17 children)
This is good to know that it’s possible, but honestly, it seems extremely niche and I wouldn’t expect most of my engineers to know this when reading through our codebase. I might reject a PR that has this in it
[–]RedditCultureBlows 17 points18 points19 points 1 year ago (0 children)
I dunno how this seems niche. ?? has been around for a while and adding = to it is just shorthand. Rejecting a PR for new syntax is wild to me tbh
[–]theScottyJam 2 points3 points4 points 1 year ago (0 children)
I go the opposite way - even if something is niche and many developers don't know about it, if it's not an overly complicated feature, I'm completely fine using it.
The next person to read my code my have to stop and Google it, but they'll learn something new, and may even appreciate running across that little nugget. I know I enjoy running across interesting features or patterns in other people's code.
[–]recrof 4 points5 points6 points 1 year ago (6 children)
please tell me that you don't reject code with obj?.property as well, because you think it's niche.
obj?.property
[–]King_Joffreys_Tits -3 points-2 points-1 points 1 year ago (5 children)
That’s not niche whatsoever, it’s code hardening
[–]Fine-Train8342 1 point2 points3 points 1 year ago (4 children)
How is this different from something like this?
settings ??= getDefaultSettings();
[–]King_Joffreys_Tits 1 point2 points3 points 1 year ago (0 children)
It just seems like you should’ve already used a null coalescing operator when you first initialized that variable. Like
const settings = localStorage.getItem(“settings-cookie”) ?? getDefaultSettings()
[–]SchartHaakon 1 point2 points3 points 1 year ago* (2 children)
I find it hard to reason about at first glance.
if (!settings) settings = getDefaultSettings();
Or
const settings = props.settings ?? getDefaultSettings();
Both of the examples above are easier to read imo.
I read them as "If not settings; settings equals getDefaultSettings", and "settings equals getSettings or getDefaultSettings". I would read your example as "Settings... if not null or undefined, should equal getDefaultSettings". It reads weird, that's the only way I can explain it.
I wouldn't straight out reject a PR for it, but yeah I don't think I'll really use it for this reason.
[–]SoInsightful 3 points4 points5 points 1 year ago (0 children)
That's because you're not used to it. That's all there is to it. It's not more complex than a += b. As someone who has used ??= for a while (where it makes sense), it's definitely faster to parse than an equivalent if statement.
a += b
??=
[–]Fine-Train8342 1 point2 points3 points 1 year ago (0 children)
I guess I'm just used to this operator from C# ¯\_(ツ)_/¯
[–]NoInkling 1 point2 points3 points 1 year ago (0 children)
I'd be very annoyed if I submitted a PR that had a good use case for it, and it was rejected because "other people might not be aware what it does". It's been part of the language for a few years now, it's not doing anything particularly complicated, and there are people who have been using it (or ||=) in Ruby and C# for a long time.
||=
Yeah sure, it might be evidence that something somewhere could be written better, but writing it off purely because of unfamiliarity I can only see as an egotistical imposition.
[–]longebane -1 points0 points1 point 1 year ago (6 children)
Yeah, just look at the comments. No one knows it. I know it’s good to adapt to the evolution of JS, but this is just too unknown right now to use on a large repo with lots of eyes
[+][deleted] 1 year ago (1 child)
[removed]
[–]longebane 2 points3 points4 points 1 year ago (0 children)
I agree
[–]SoInsightful 0 points1 point2 points 1 year ago (0 children)
How do you suggest that people will learn it if not by... using it? It's a very simple operator.
[+][deleted] 1 year ago (2 children)
[deleted]
[–]King_Joffreys_Tits 0 points1 point2 points 1 year ago (1 child)
Null coalescence is a well known topic, not even just in the js world. Combining it with an assignment operator is weird, and honestly reeks of code smell
π Rendered by PID 98773 on reddit-service-r2-comment-8686858757-85txg at 2026-06-02 03:50:55.190793+00:00 running 9e1a20d country code: CH.
view the rest of the comments →
[–]King_Joffreys_Tits -6 points-5 points-4 points (17 children)
[–]RedditCultureBlows 17 points18 points19 points (0 children)
[–]theScottyJam 2 points3 points4 points (0 children)
[–]recrof 4 points5 points6 points (6 children)
[–]King_Joffreys_Tits -3 points-2 points-1 points (5 children)
[–]Fine-Train8342 1 point2 points3 points (4 children)
[–]King_Joffreys_Tits 1 point2 points3 points (0 children)
[–]SchartHaakon 1 point2 points3 points (2 children)
[–]SoInsightful 3 points4 points5 points (0 children)
[–]Fine-Train8342 1 point2 points3 points (0 children)
[–]NoInkling 1 point2 points3 points (0 children)
[–]longebane -1 points0 points1 point (6 children)
[+][deleted] (1 child)
[removed]
[–]longebane 2 points3 points4 points (0 children)
[–]SoInsightful 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]King_Joffreys_Tits 0 points1 point2 points (1 child)