In certain computer programming languages, the Elvis operator, often written
||, is a binary operator that returns its first operand if that operand evaluates to a true value, and otherwise evaluates and returns its second operand. This is identical to a short-circuit or with "last value" semantics. The notation of the Elvis operator was inspired by the ternary conditional operator,
? : since the Elvis operator expression
A ?: B is approximately equivalent to the ternary conditional
A ? A : B.
A similar operator is the null coalescing operator, where the check for boolean truthiness is replaced with a check for non-null instead. This is usually written
??, and can be seen in languages like C#.
In a language that supports the Elvis operator, something like this:
x = f() ?: g()
x equal to the result of
f() if that result is a true value, and to the result of
It is equivalent to this example, using the conditional ternary operator:
x = f() ? f() : g()
except that it does not evaluate the
f() twice if it is true.
Object reference variant
This code will result in a reference to an object that is guaranteed to not be null. Function
f() returns an object reference instead of a boolean, and may return null:
x = f() ?: "default value"
Languages supporting the Elvis operator
- In GNU C and C++ (that is: in C and C++ with GCC extensions), the second operand of the ternary operator is optional. This has been the case since at least GCC 2.95.3 (March 2001), and seems to be the original elvis operator.
- In Apache Groovy, the "Elvis operator"
?:is documented as a distinct operator; this feature was added in Groovy 1.5 (December 2007). Groovy, unlike GNU C and PHP, does not simply allow the second operand of ternary
?:to be omitted; rather, binary
?:must be written as a single operator, with no whitespace in between.
- In PHP, it is possible to leave out the middle part of the ternary operator since PHP 5.3. (June 2009).
- The Fantom programming language has the
?:binary operator that compares its first operand with
- In Kotlin, the Elvis operator returns its left-hand side if it is not null, and its right-hand side otherwise. A common pattern is to use it with
return, like this:
val foo = bar() ?: return
- In Gosu, the
?:operator returns the right operand if the left is null as well.
- In C#, the null-conditional operator,
?.is referred to as the "Elvis operator", but it does not perform the same function. Instead, the null-coalescing operator
- In ColdFusion and CFML, the Elvis operator was introduced using the
- The Xtend programming language has an Elvis operator.
- In Google's Closure Templates, the Elvis operator is a null coalescing operator, equivalent to
isNonnull($a) ? $a : $b.
- Swift supports this concept with its Nil-coalescing operator
(a ?? b).
- SQL supports this concept with its COALESCE function, e.g.
- In Ballerina, the Elvis operator
L ?: Rreturns the value of
Lif it's not nil. Otherwise, return the value of
- Clojure supports this concept with the
or macro, e.g.
(or a b). In the case of Clojure, it is var-arg, and not binary, e.g.
(or a b c d e)will return the first non false value.
- Dart language provides ?? operator which returns right side value if left side value is null
- TypeScript supports this concept with its nullish-coalescing operator
(a ?? b), since v3.7.
- Lua supports this concept with the
or logical operator, e.g.
(a or b).
Analogous use of the short-circuiting OR operator
or) has the same behavior as the above: returning its first operand if it would evaluate to true in a boolean environment, and otherwise evaluating and returning its second operand. When the left hand side is true, the right hand side is not even evaluated; it is "short-circuited." This is different than the behavior in other languages such as C/C++, where the result of
|| will always be a boolean.
?:or conditional operator, when used as a ternary operator
- Safe navigation operator, often
- Spaceship operator
- Option type
- Joyce Farrell. Java Programming. p. 276. ISBN 978-1285081953.
The new operator is called Elvis operator because it uses a question mark and a colon together (?:); if you view it sideways, it reminds you of Elvis Presley.
- "?? Operator". C# Reference. Microsoft. Retrieved 5 December 2018.
- "Using the GNU Compiler Collection (GCC): Conditionals". gcc.gnu.org.
- "Using and Porting the GNU Compiler Collection (GCC): C Extensions". gcc.gnu.org.
- "Elvis Operator (?: )".
- "The Apache Groovy programming language - Groovy 1.5 release notes". groovy-lang.org.
- "PHP: Comparison Operators - Manual". PHP website. Retrieved 2014-02-17.
- "Null Safety - Kotlin Programming Language". Kotlin.
- Albahari, Joseph; Albahari, Ben (2015). C# 6.0 in a Nutshell (6 ed.). O'Reilly Media. p. 59. ISBN 978-1491927069.
- Efftinge, Sven. "Xtend - Expressions". eclipse.org.
- "Closure Templates - Expressions". GitHub.
- "The Swift Programming Language (Swift 4.1): Basic Operators". developer.apple.com.
- "Elvis Operator - Ballerina Programming Language". Ballerina.
- "clojure.core or macro API reference".
- "nullish coalescing commit by Kingwl · Pull Request #32883 · microsoft/TypeScript". GitHub. Retrieved 2019-10-08.
- "Nullish coalescing operator (??)". mozilla. Retrieved 2021-01-28.
- "Lua or operator Reference".