Free JavaScript reference · Python timeline
JavaScript ES2021: Defaults, literal replacements, and first success
Logical assignment avoids repeated access, replaceAll handles every occurrence, and Promise.any resolves on the first fulfillment.
Edition 12 · Published · Editorial review October 26, 2026
Standardized in ES2021 · Syntax
Logical assignment operators
??=, ||=, and &&= combine short-circuiting decisions with assignment. The target is assigned only when the corresponding condition requires the right-hand side.
Use ??= for nullish defaults and ||= when all falsey values mean a default is needed. They are not interchangeable when zero or empty text is meaningful. Unlike rewriting the full property access twice, these operators evaluate the left-hand reference once.
The example preserves a zero retry setting but replaces an empty title under an explicit falsey-value policy.
Worked example
Uses ES2021 features · Script
const options = {retries: 0, title: ""}; options.retries ??= 3; options.title ||= "Untitled"; console.log(options.retries, options.title);
Expected output
0 Untitled
Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.
Common mistake
Choosing ||= can overwrite a meaningful false or zero. Assignment can also fail for a read-only target when its condition triggers.
Try it yourself
Replace ??= with ||= and predict the retry count. Which policy better matches a disabled-retry setting?
Read more: ES2021 specification · MDN explanation and compatibility
Standardized in ES2021 · Built-in method
replaceAll() with a literal search
replaceAll replaces every occurrence of a literal string search without requiring you to construct a regular expression. This is useful when punctuation in user-supplied search text should remain literal.
If you supply a regular expression, it must have the global flag. The method returns a new string, leaving the original contents intact. Replacement text has its own special sequences, so use a replacement function when a replacement value must be inserted literally.
The example searches for a dot-containing token and inserts a simple literal replacement.
Worked example
Uses ES2021 features · Script
const text = "a.b and a.b"; console.log(text.replaceAll("a.b", "item")); console.log(text);
Expected output
item and item
a.b and a.b
Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.
Common mistake
A string search is literal, but replacement-string $ sequences still have meaning. A non-global RegExp argument throws.
Try it yourself
Try a replacement containing $& and compare a string replacement with () => "$&".
Read more: ES2021 specification · MDN explanation and compatibility
Standardized in ES2021 · Built-in async API
Promise.any() selects the first fulfillment
Promise.any fulfills with the first input that fulfills, ignoring earlier rejections while another success is still possible. It is useful when any one acceptable result is enough.
This differs from race, which settles on the first fulfillment or rejection. If every input rejects, any rejects with an AggregateError whose errors describe the failed inputs. An empty input also rejects.
The example succeeds despite a rejected input. The other operations are not automatically canceled after a winner is chosen.
Worked example
Uses ES2021 features · Script
const finished = Promise.any([ Promise.reject("unavailable"), Promise.resolve("ready") ]).then(value => console.log(value));
Expected output
ready
Example checked with Chromium 151.0.7922.34, Node.js 24.19.0.
Common mistake
A winner does not cancel the losing work. Plan resource cleanup separately when operations have costs or side effects.
Try it yourself
Make both inputs reject and inspect AggregateError.errors in a catch handler.
Read more: ES2021 specification · MDN explanation and compatibility
Recap
Logical assignment avoids repeated access, replaceAll handles every occurrence, and Promise.any resolves on the first fulfillment. Check the engines your project supports before adopting new syntax or built-ins.
Read the ES2021 specification →