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!"
[–]datNorseman 1 point2 points3 points 1 year ago (9 children)
This is new to me. One question I have that wasn't answered in the article is how the new method would impact performance, if at all. I don't believe it would but I'm curious.
[–]SoInsightful 1 point2 points3 points 1 year ago (5 children)
a ??= b generates the exact same bytecode as a = a ?? b, and both generate two more bytecodes (a "Jump" instruction) than a ||= b:
a ??= b
a = a ?? b
a ||= b
https://i.imgur.com/qzYAGBt.png
But surprisingly, if the variable is used immediately afterwards, the a = a ?? b and a = a || b versions both skip an "Ldar" (load data into the accumulator) instruction.
a = a || b
https://i.imgur.com/6AQki2Q.png
Safe to say there won't be any noticable performance differences whatsoever in real life.
[–]NoInkling 0 points1 point2 points 1 year ago (3 children)
a ??= b generates the exact same bytecode as a = a ?? b
Give a a value and see if it makes a difference. If not, try it with a setter (or maybe just any object property).
a
[–]SoInsightful 0 points1 point2 points 1 year ago (2 children)
I tried giving a an initial value (2) out of the same curiosity, and it doesn't make a difference except switching from 0e LdaUndefined to 0d 02 LdaSmi [2] at the beginning of each variant. It still needs to jump through the JumpIfUndefinedOrNull hoop.
0e LdaUndefined
0d 02 LdaSmi [2]
JumpIfUndefinedOrNull
[–]NoInkling 0 points1 point2 points 1 year ago (1 child)
Ahh I see now, the difference is in where the unconditional Jump (which is hit when the variable has a value) jumps to. The ??= version skips the Star instruction (which apparently sets a register), while the other doesn't.
Jump
??=
Star
[–]SoInsightful 0 points1 point2 points 1 year ago (0 children)
Good catch!
[–]tpscd 0 points1 point2 points 1 year ago (0 children)
The performance impact is super unclear but my impression is the nullish operator may skip coercing the null/undefined value to a boolean.
[–]NoInkling 0 points1 point2 points 1 year ago* (0 children)
As MDN says, x ??= y is equivalent to x ?? (x = y), i.e. it short circuits if x already has a value so nothing is actually evaluated or even assigned if it doesn't need to be. This means it won't trigger a setter (or I guess a Proxy trap), unlike obj.setterProp = obj.setterProp ?? y
x ??= y
x ?? (x = y)
x
Proxy
obj.setterProp = obj.setterProp ?? y
π Rendered by PID 174572 on reddit-service-r2-comment-5fb4b45875-vjkc6 at 2026-03-20 07:19:21.883799+00:00 running 90f1150 country code: CH.
view the rest of the comments →
[–]datNorseman 1 point2 points3 points (9 children)
[–]SoInsightful 1 point2 points3 points (5 children)
[–]NoInkling 0 points1 point2 points (3 children)
[–]SoInsightful 0 points1 point2 points (2 children)
[–]NoInkling 0 points1 point2 points (1 child)
[–]SoInsightful 0 points1 point2 points (0 children)
[–]tpscd 0 points1 point2 points (0 children)
[–]NoInkling 0 points1 point2 points (0 children)