LLMpediaThe first transparent, open encyclopedia generated by LLMs

Nullish coalescing operator

Generated by GPT-5-mini
Note: This article was automatically generated by a large language model (LLM) from purely parametric knowledge (no retrieval). It may contain inaccuracies or hallucinations. This encyclopedia is part of a research project currently under review.
Article Genealogy
Parent: TC39 Hop 4
Expansion Funnel Raw 54 → Dedup 0 → NER 0 → Enqueued 0
1. Extracted54
2. After dedup0 (None)
3. After NER0 ()
4. Enqueued0 ()
Nullish coalescing operator
NameNullish coalescing operator
Introduced2019
LanguagesJavaScript, TypeScript, C#, PHP (null coalescing), Swift (nil-coalescing)
Examplea ?? b

Nullish coalescing operator The nullish coalescing operator is a short-circuiting conditional operator used in several programming languages to select the first operand that is not null or undefined. It provides a concise way to express fallbacks and defaults, closely related to language features in Microsoft-affiliated languages and modern web development ecosystems like Node.js, React, and Angular. The operator interacts with language specifications and tooling from organizations such as Ecma International, ISO/IEC, and companies like Google and Apple that influence platform implementations.

Overview

The operator returns the left-hand operand when it is neither null nor undefined; otherwise it returns the right-hand operand. It was standardized in the ECMAScript proposals accepted by TC39 and implemented across runtimes maintained by groups including Mozilla Corporation and Google through engines like SpiderMonkey and V8. Early adopters in developer communities from GitHub projects to packages in npm catalyzed cross-ecosystem usage alongside frameworks such as Vue.js and Svelte. Its semantics echo earlier constructs in languages designed or influenced by organizations like Microsoft (for C#) and standardization bodies such as ISO/IEC JTC 1.

Syntax and semantics

Typical syntax uses a dedicated token between two expressions, for example a ?? b in languages following the ECMAScript proposal. The operator performs a strict nullish test: it treats null and undefined as nullish while treating values like 0, empty string, and false as valid. The operator’s short-circuiting behavior resembles conditional operators found in languages developed by entities such as Sun Microsystems (now part of Oracle Corporation) and designs from Bjarne Stroustrup's work on C++. Specification-level semantics are discussed in documents produced by TC39, ECMA-262, and motivated by compatibility with runtime engines implemented by vendors such as Microsoft's Chakra (historically) and contemporary standards in Apple’s JavaScriptCore.

Comparison with other operators

The nullish coalescing operator is contrasted frequently with the logical OR operator (||). Where || was implemented in engines like V8 and historically used in Node.js-based projects, it treats falsy values such as 0 and "" as failing, which differs from the nullish test. It parallels the null coalescing operator introduced in C# by Microsoft and the nil-coalescing operator in Swift promoted by Apple. In web frameworks such as React and Angular, developers often choose between ||, ternary expressions influenced by Brendan Eich’s language design, and ?? depending on intent. Language design debates recorded in TC39 proceedings echo concerns raised in committees like ISO/IEC JTC 1/SC 22 about backward compatibility and clarity.

Examples and usage patterns

Common patterns include defaulting configuration values in applications maintained on GitHub and in packages published to npm. Example idioms appear in tutorials from organizations like Mozilla Foundation and educational material referencing contributors such as Kyle Simpson and Addy Osmani. Developers working with frontend stacks combining React, Redux, and Next.js often use the operator to provide safe fallbacks for API responses. Backend examples in Node.js and serverless deployments on AWS Lambda or Google Cloud Platform use it to normalize missing metadata. In libraries influenced by TypeScript authors like Anders Hejlsberg or ecosystems curated by Microsoft teams, the operator appears alongside type narrowing and control-flow analysis.

Language support and implementation

Adoption spans mainstream engines and languages: JavaScript (via ECMAScript), TypeScript (compiler support), C# (null-coalescing ??), PHP (null coalescing ?? in recent versions), and Swift (nil coalescing ??), with runtime implementations in engines like V8, SpiderMonkey, and JavaScriptCore. Compiler and transpiler tooling from projects such as Babel and Webpack enable backward compatibility for older browsers maintained by vendors including Microsoft (Internet Explorer legacy) and Apple (Safari). Language committees and implementers—TC39, ECMA International, ISO/IEC groups, and corporate engineering teams at Google and Microsoft—coordinate specification details, test262 suites, and conformance tests.

Common pitfalls and best practices

Pitfalls include accidental semantics when migrating code that previously relied on || to provide defaults for falsy values like 0 or "". Best practices recommended by authors from organizations such as Mozilla Foundation and contributors on Stack Overflow and GitHub include preferring ?? when only null or undefined should trigger defaults, combining ?? with optional chaining introduced in ECMAScript proposals, and ensuring linter rules from projects like ESLint reflect intended behavior. When interoperating with libraries from ecosystems curated by npm maintainers or frameworks by teams at Facebook, Inc. (now Meta Platforms, Inc.) and Google, developers should document expectations for nullish behavior to avoid regressions in codebases managed by continuous integration systems such as Jenkins or GitHub Actions.

Category:Programming language operators