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, 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.[src] It is most commonly written as x ??[src] y , but varies across programming languages.[src] 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.[src] This behavior allows a default value to be defined for cases where a more specific value is not available.[src] 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.[src]
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.[src] It is most commonly written as x ??[src] y , but varies across programming languages.[src] 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.[src] This behavior allows a default value to be defined for cases where a more specific value is not available.[src] 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.[src] supplied_title='supplied title' # Uncomment this line to use the supplied title title=${supplied_title:-'Default title'} echo "$title" # prints: Default title possiblyNullValue ??[src] 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: string pageTitle = suppliedTitle ??[src] "Default Title"; instead of the more verbose string pageTitle = (suppliedTitle != null) ?[src] suppliedTitle : "Default Title"; or string pageTitle; if (suppliedTitle != null) { pageTitle = suppliedTitle; } else { pageTitle = "Default Title"; } The three forms result in the same value being stored into the variable named pageTitle .[src] suppliedTitle is referenced only once when using the ??[src] operator, and twice in the other two code examples.[src] The operator can also be used multiple times in the same expression: return some_Value ??[src] some_Value2 ??[src] some_Value3; 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.[src] 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 ??[src] some_Value2; In combination with the null-conditional operator ?.[src] 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.[src] 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 ??[src] "Default Title"; possiblyNullValue ?: valueIfNull ${missingVariable!"defaultValue"} or, to leave the output blank: ${missingVariable!} const a = b ??[src] 3; Before the nullish coalescing operator, programmers would use the logical OR operator ( || ).[src] But where ??[src] looks specifically for null or undefined , the || operator looks for any falsy value: null , undefined , "" , 0 , NaN , and of course, false .[src] 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.[src] const a = b || 3; val title = suppliedTitle ?: "Default title" id value = valueThatMightBeNil ?: valueIfNil; This is the same as writing id value = valueThatMightBeNil ?[src] valueThatMightBeNil : valueIfNil; $possibly_null_value // $value_if_null The possibly_null_value is evaluated as null or not-null (in Perl terminology, undefined or defined).[src] On the basis of the evaluation, the expression returns either value_if_null when possibly_null_value is null, or possibly_null_value otherwise.[src] In the absence of side-effects this is similar to the way ternary operators ( ?: statements) work in languages that support them.[src] The above Perl code is equivalent to the use of the ternary operator below: defined($possibly_null_value) ?[src] $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.[src] Perl additionally has a //= assignment operator, where $a //= $b is largely equivalent to: $a = $a // $b This operator differs from Perl's older || and ||= operators in that it considers definedness, not truth.[src] Thus they behave differently on values that are false but defined, such as 0 or "" (a zero-length string): $a = 0; $b = 1; $c = $a // $b; # $c = 0 $c = $a || $b; # $c = 1 $name = $request->input['name'] ??[src] $request->query['name'] ??[src] 'default name'; /* Equivalent to */ if (isset($request->input['name'])) { $name = $request->input['name']; } elseif (isset($request->query['name'])) { $name = $request->query['name']; } else { $name = 'default name'; } $user = $this->getUser() ??[src] $this->createGuestUser(); /* Equivalent to */ $user = $this->getUser(); if ($user === null) { $user = $this->createGuestUser(); } $pageTitle = $title ??[src] 'Default Title'; /* Equivalent to */ $pageTitle = isset($title) ?[src] $title : 'Default Title'; Version 7.4 of PHP introduced the Null Coalescing Assignment Operator with the ??= syntax: // The following lines are doing the same $this->request->data['comments']['user_id'] = $this->request->data['comments']['user_id'] ??[src] 'value'; // Instead of repeating variables with long names, the equal coalesce operator is used $this->request->data['comments']['user_id'] ??= 'value'; $myVar = $null $x = $myVar ??[src] "something" # assigns "something" > NULL %||% 2 [1] 2 // Option // An Option can be either Some(value) or None Some(1).unwrap_or(0); // evaluates to 1 None.unwrap_or(0); // evaluates to 0 None.unwrap_or_else(get_default); // evaluates to the result of calling the function get_default // Result // A Result can be either Ok(value) or Err(error) Ok(1).unwrap_or(0); // evaluates to 1 Err("oh no").unwrap_or(1); // evaluates to 1 NVL(possibly_null_value, 'value if null'); In SQL Server/Transact-SQL there is the ISNULL function that follows the same prototype pattern: ISNULL(possibly_null_value, 'value if null'); 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.[src] The ANSI SQL-92 standard includes the COALESCE function implemented in Oracle, SQL Server, PostgreSQL, SQLite and MySQL.[src] The COALESCE function returns the first argument that is not null.[src] If all terms are null, returns null.[src] COALESCE(possibly_null_value[, possibly_null_value, ...]); The difference between ISNULL and COALESCE is that the type returned by ISNULL is the type of the leftmost value while COALESCE returns the type of the first non-null value.[src] optionalValue ??[src] 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?[src] = ...[src] var pageTitle: String = suppliedTitle ??[src] "Default Title" instead of the more verbose var pageTitle: String = (suppliedTitle != nil) ?[src] suppliedTitle![src] : "Default Title";[src]
56 sentences · 56 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Null coalescing operator." encyclopegia.org, 2026, https://encyclopegia.org/wiki/null-coalescing-operator. Accessed 02 December 2026.
Chicago
"Null coalescing operator." encyclopegia.org. Accessed December 2, 2026. https://encyclopegia.org/wiki/null-coalescing-operator.
APA
encyclopegia.org. (2026). Null coalescing operator. Retrieved December 2, 2026, from https://encyclopegia.org/wiki/null-coalescing-operator
Accessed December 2, 2026 · CC BY-SA 4.0 · citation page · JSON