ArticleslgStudy

science

Null coalescing operator

Null coalescing operator is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Null coalescing operator rather than just read about it. In short: The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since version 7.0.0, Perl since version 5.10 as logical defined-or, PowerShell since 7.0.0, and Swift as nil-coalescing operator. It is most commonly written as x ?? y, but varies across…

Key takeaways

  • Null coalescing operator belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Null coalescing operator to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Null coalescing operator from memory before moving on to harder problems.

Reference excerpt

The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since version 7.0.0, Perl since version 5.10 as logical defined-or, PowerShell since 7.0.0, and Swift as nil-coalescing operator. It is most commonly written as x ?? y, but varies across programming languages. While its behavior differs between implementations, the null coalescing operator generally returns the result of its left-most operand if it exists and is not null, and otherwise returns the right-most operand. This behavior allows a default value to be defined for cases where a more specific value is not available. Like the binary Elvis operator, usually written as x ?: y, the null coalescing operator is a short-circuiting operator and thus does not evaluate the second operand if its value is not used, which is significant if its evaluation has side-effects.

Examples by languages

Bourne-like shells In Bourne shell (and derivatives), "If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted":

C# In C#, the null coalescing operator is ??. It is most often used to simplify expressions as follows:

For example, if one wishes to implement some C# code to give a page a default title if none is present, one may use the following statement:

instead of the more verbose

or

The three forms result in the same value being stored into the variable named pageTitle. suppliedTitle is referenced only once when using the ?? operator, and twice in the other two code examples. The operator can also be used multiple times in the same expression:

Once a non-null value is assigned to number, or it reaches the final value (which may or may not be null), the expression is completed. If, for example, a variable should be changed to another value if its value evaluates to null, since C# 8.0 the ??= null coalescing assignment operator can be used:

Which is a more concise version of:

In combination with the null-conditional operator ?. or the null-conditional element access operator ?[] the null coalescing operator can be used to provide a default value if an object or an object's member is null. For example, the following will return the default title if either the page object is null or page is not null but its Title property is:

CFML As of ColdFusion 11, Railo 4.1, CFML supports the null coalescing operator as a variation of the ternary operator, ?:. It is functionally and syntactically equivalent to its C# counterpart, above. Example:

Freemarker Missing values in Apache FreeMarker will normally cause exceptions. However, both missing and null values can be handled, with an optional default value:

or, to leave the output blank:

JavaScript JavaScript's nearest operator is ??, the "nullish coalescing operator", which was added to the standard in ECMAScript's 11th edition. In earlier versions, it could be used via a Babel plugin, and in TypeScript. It evaluates its left-hand operand and, if the result value is not "nullish" (null or undefined), takes that value as its result; otherwise, it evaluates the right-hand operand and takes the resulting value as its result. In the following example, a will be assigned the value of b if the value of b is not null or undefined, otherwise it will be assigned 3.

Before the nullish coalescing operator, programmers would use the logical OR operator (||). But where ?? looks specifically for null or undefined, the || operator looks for any falsy value: null, undefined, "", 0, NaN, and of course, false. In the following example, a will be assigned the value of b if the value of b is truthy, otherwise it will be assigned 3.

Kotlin Kotlin uses the ?: operator. This is an unusual choice of symbol, given that ?: is typically used for the Elvis operator, not null coalescing, but it was inspired by Groovy (programming language) where null is considered false.

Objective-C In Obj-C, the nil coalescing operator is ?:. It can be used to provide a default for nil references:

This is the same as writing

Perl In Perl (starting with version 5.10), the operator is // and the equivalent Perl code is:

The possibly_null_value is evaluated as null or not-null (in Perl terminology, undefined or defined). On the basis of the evaluation, the expression returns either value_if_null when possibly_null_value is null, or possibly_null_value otherwise. In the absence of side-effects this is similar to the way ternary operators (?: statements) work in languages that support them. The above Perl code is equivalent to the use of the ternary operator below:

This operator's most common usage is to minimize the amount of code used for a simple null check.

Perl additionally has a //= assignment operator, where is largely equivalent to: This operator differs from Perl's older || and ||= operators in that it considers definedness, not truth. Thus they behave differently on values that are false but defined, such as 0 or "" (a zero-length string):

PHP PHP 7.0 introduced a null-coalescing operator with the ?? syntax. This checks strictly for NULL or a non-existent variable/array index/property. In this respect, it acts similarly to PHP's isset() pseudo-function:

Version 7.4 of PHP introduced the Null Coalescing Assignment Operator with the ??= syntax:

PowerShell Since PowerShell 7, the ?? null coalescing operator provides this functionality.

R Since R version 4.4.0 the %||% operator is included in base R (previously it was a feature of some packages like rlang).

Rust While there's no null in Rust, tagged unions are used for the same purpose. For example, Result<T, E> or Option<T>. Any type implementing the Try trait can be unwrapped. unwrap_or() serves a similar purpose as the null coalescing operator in other languages. Alternatively, unwrap_or_else() can be used to use the result of a function as a default value.

SQL In Oracle's PL/SQL, the NVL() function provides the same outcome:

In SQL Server/Transact-SQL there is the ISNULL function that follows the same prototype pattern:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Null coalescing operator

Start with the simplest possible case. Write down what Null coalescing operator claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Null coalescing operator before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Null coalescing operator ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Null coalescing operator

In research
Null coalescing operator appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Null coalescing operator in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Null coalescing operator is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary operations, Conditional constructs, Operators (programming), so understanding it makes those chapters shorter.
In everyday life
Look for Null coalescing operator outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Null coalescing operator” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Null coalescing operator in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Null coalescing operator means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Null coalescing operator out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Null coalescing operator in simple terms?

The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since version 7.0.0, Perl since version 5.10 as logical defined-or…

Why does Null coalescing operator matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Null coalescing operator?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Null coalescing operator.

Tags

  • Binary operations
  • Conditional constructs
  • Operators (programming)

Keep exploring