Null coalescing operator
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, including C#,, PowerShell as of version 7.0.0, Perl as of version 5.10, Swift, and PHP 7.0.0. 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.
In contrast to the ternary operator used as
x ? x : y
, but like the binary Elvis operator used as x ?: y
, the null coalescing operator is a binary operator and thus evaluates its operands at most once, which is significant if the evaluation of x
has side-effects.Examples by languages
Bash
In Bash "If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted":- supplied_title='supplied title' # uncomment this line to use the supplied title
echo "$title" # prints: Default title
C#
In C#, the null coalescing operator is??
.It is most often used to simplify expressions as follows:
possiblyNullValue ?? valueIfNull
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
string pageTitle = ? suppliedTitle : "Default Title";
or
string pageTitle;
if else
The three forms result in the same value being stored into the variable named
pageTitle
.Note that
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:
return some_Value ?? some_Value2 ?? some_Value3;
Once a non-null value is assigned to number, or it reaches the final value, 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:some_Value ??= some_Value2;
Which is a more concise version of:
some_Value = some_Value ?? some_Value2;
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:string pageTitle = page?.Title ?? "Default Title";
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:possiblyNullValue ?: valueIfNull
Clojure
Clojure'sor
macro can be used similarly, because it returns the first non false value, and nil is considered false in Clojure. The inclusion of false makes it slightly different to traditional Elvis operators.You can also chain values.
;; returns first not-false value or nil
Be careful if you care about distinguishing between false and nil in this case, since
or
does not.F#
The null value is not normally used in F# for values or variables. However null values can appear for example when F# code is called from C#.F# does not have a built-in null coalescing operator but one can be defined as required as a custom operator:
let lhs rhs =
This custom operator can then be applied as per C#'s built-in null coalescing operator:
let pageTitle = suppliedTitle |? "Default Title"
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:
Haskell
Types in Haskell can in general not be null. Representation of computations that may or may not return a meaningful result is represented by the generic Maybe type, defined in the standard library asdata Maybe a = Nothing | Just a
The null coalescing operator replaces null pointers with a default value. The Haskell equivalent is a way of extracting a value from a Maybe by supplying a default value. This is the function fromMaybe.
fromMaybe :: a -> Maybe a -> a
fromMaybe d x = case x of
Some example usage follows.
fromMaybe 0 -- returns 3
fromMaybe "" -- returns ""
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 evalutes its left-hand operand and, if the result value is not "nullish", 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.const a = b ?? 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.const a = b || 3;
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 where null is considered false.val title = suppliedTitle ?: "Default title"
Objective-C
In Obj-C, the nil coalescing operator is?:
. It can be used to provide a default for nil references:id value = valueThatMightBeNil ?: valueIfNil;
This is the same as writing
id value = valueThatMightBeNil ? valueThatMightBeNil : valueIfNil;
Perl
In Perl, the operator is//
and the equivalent Perl code is:$possibly_null_value // $value_if_null
The possibly_null_value is evaluated as null or not-null. 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 work in languages that support them. The above Perl code is equivalent to the use of the ternary operator below:
defined ? $possibly_null_value : $value_if_null
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 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 = 0;
$b = 1;
$c = $a // $b; # $c = 0
$c = $a || $b; # $c = 1
PHP
PHP 7 has 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:$name = $request->input ?? $request->query ?? 'default name';
/* Equivalent to */
if elseif else
$user = $this->getUser ?? $this->createGuestUser;
/* Equivalent to */
$user = $this->getUser;
if
$pageTitle = $title ?? 'Default Title';
/* Equivalent to */
$pageTitle = isset ? $title : 'Default Title';
Version 7.4 of PHP will add the Null Coalescing Assignment Operator with the
??=
syntax:// The following lines are doing the same
$this->request->data = $this->request->data ?? 'value';
// Instead of repeating variables with long names, the equal coalesce operator is used
$this->request->data ??= 'value';
Python
The operator provides this functionality.other = s or "some default value"
Note that the operator does not return only or. Instead, it returns the first operand if the first operand evaluates to true, and it returns the second operand if the first operand evaluates to false.
In this case, the expression returns if it is or evaluates to true when converted to boolean. Otherwise, it returns. For most cases, this will serve for the very same purpose of C#'s null-coalescing operator, but keep in mind:
42 or "something" # returns 42
0 or "something" # returns "something"
None or "something" # returns "something"
False or "something" # returns "something"
"" or "something" # returns "something"
There was a proposal to add a new operator or operators to handle this differently in Python 3.8, but that proposal has been deferred.
Rust
While there's nonull
in Rust, tagged unions are used for the same purpose. For example, Result
or Option
.unwrap_or
serves a similar purpose as the null coalescing operator in other languages.let parsed_numbers: Vec<_> =
.iter
.map.unwrap_or)
.collect;
// prints ""
println!;
SQL
In Oracle's PL/SQL, the NVL function provides the same outcome:NVL;
In SQL Server/Transact-SQL there is the ISNULL function that follows the same prototype pattern:
ISNULL;
Attention should be taken to not confuse ISNULL with IS NULL – the latter serves to evaluate whether some contents are defined to be NULL or not.
The ANSI SQL-92 standard includes the COALESCE function implemented in Oracle, SQL Server, PostgreSQL, SQLite and MySQL. The COALESCE function returns the first argument that is not null. If all terms are null, returns null.
COALESCE;
Swift
In Swift, the nil coalescing operator is??
. It is used to provide a default when unwrapping an optional type:optionalValue ?? valueIfNil
For example, if one wishes to implement some Swift code to give a page a default title if none is present, one may use the following statement:
var suppliedTitle: String? =...
var pageTitle: String = suppliedTitle ?? "Default Title"
instead of the more verbose
var pageTitle: String = ? suppliedTitle! : "Default Title";
VB.NET
In VB.NET theIf
operator/keyword achieves the null coalescing operator effect.Dim pageTitle = If
which is a more concise way of using its variation
Dim pageTitle = If