Shortest way to check for null and assign another value if not
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Most modern languages provide a null coalescing operator that returns a default value when the expression is null. In C# it is ??, in JavaScript ??, in Python or (with caveats), in Kotlin ?:, and in Swift ??. These operators replace verbose if (x == null) checks with a single concise expression, making null-handling code shorter and more readable.
C# — Null Coalescing Operator (??)
?? only checks for null. Unlike JavaScript's ||, it does not treat "", 0, or false as needing a default.
JavaScript — Nullish Coalescing (??)
Python — or Operator (with Caveats)
Python has no dedicated null coalescing operator. The or operator works for strings and objects but fails for 0, "", False, and other falsy values.
Kotlin — Elvis Operator (?:)
Swift — Nil Coalescing Operator (??)
PHP — Null Coalescing Operator (??)
Ruby — Conditional Assignment (||=)
Comparison Table
| Language | Operator | Checks for | Assignment form | ||||
| C# | ?? | null | ??= | ||||
| JavaScript | ?? | null, undefined | ??= | ||||
| Python | or | All falsy values | N/A | ||||
| Kotlin | ?: | null | N/A | ||||
| Swift | ?? | nil | N/A | ||||
| PHP | ?? | null, unset | ??= | ||||
| Ruby | `\ | \ | ` | nil, false | `\ | \ | =` |
Common Pitfalls
- Using
||instead of??in JavaScript:||treats0,"",false, andNaNas falsy and returns the right-hand side.??only triggers onnullandundefined. For numeric defaults,count || 10gives10when count is0, which is usually wrong. - Python
ortreating0and""as needing defaults:value or defaultreturnsdefaultwhen value is0,"",[], or any falsy value. Usevalue if value is not None else defaultfor a true null check. - Null coalescing with side effects: The right-hand expression is only evaluated if the left is null (lazy evaluation in most languages). But if the right side has side effects (incrementing a counter, making an API call), the behavior depends on whether the left is null — this makes code harder to reason about.
- Chaining too many operators:
a ?? b ?? c ?? d ?? "default"is technically valid but makes it hard to understand the fallback chain. Consider extracting into a function or using a collection-based approach like[a, b, c, d].first { it != null }. - Forgetting that
??=does not exist in all languages: Kotlin, Swift, and Python do not have a null coalescing assignment operator. Kotlin usesvalue = value ?: defaultand Swift usesif value == nil { value = default }.
Summary
- C#
??, JavaScript??, Kotlin?:, Swift??, and PHP??are null coalescing operators - They return the right-hand value only when the left is null (or nil/undefined)
- JavaScript's
??differs from||—??only checks null/undefined,||checks all falsy values - Python's
oris not a true null coalescing operator — it treats all falsy values as needing a default - Use
??=(C# 8+, JS ES2021, PHP 7.4+) for null coalescing assignment - Prefer
??over||when0,"", orfalseare valid values that should not be replaced
Related reading
- Should a return statement be inside or outside a lock?
- Should I add async/await to a single-line function or not?
- Should I avoid the use of set(Preferred|Maximum|Minimum) size methods in Java Swing?
- Should I close the channel/connection after every publish?
- Should I initialize variable within constructor or outside constructor
- Should I offload work to other threads in ASP.NET?
- Should I Stop Stopwatch at the end of the method?
- Should I use import os.path or import os?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.