In certain computer programming languages, the Elvis operator, often written ?:
, is a binary operator that evaluates its first operand and returns it if its value is logically true (according to a language-dependent convention, in other words, a truthy value), and otherwise evaluates and returns its second operand. The second operand is only evaluated if it is to be returned (short-circuit evaluation). 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 expression A ? A : B
.
The name "Elvis operator" refers to the fact that when its common notation, ?:
, is viewed sideways, it resembles an emoticon of Elvis Presley with his signature hairstyle. [1]
A similar operator is the null coalescing operator, where the boolean truth(iness) check is replaced with a check for non-null instead. This is usually written ??
, and can be seen in languages like C# [2] or Dart. [3]
In several languages, such as Common Lisp, Clojure, Lua, Object Pascal, Perl, Python, Ruby, and JavaScript, there is no need for the Elvis operator, because the language's logical disjunction operator (typically ||
or or
) is short-circuiting and returns its first operand if it would evaluate to a truthy value, and otherwise its second operand, which may be a truthy or falsy value (rather than a Boolean true or false value, such as in C and C++). These semantics are identical to the Elvis operator.
In a language that supports the Elvis operator, something like this:
x = f() ?: g()
will set x
equal to the result of f()
if that result is truthy, and to the result of g()
otherwise.
It is equivalent to this example, using the conditional ternary operator:
x = f() ? f() : g()
except that it does not evaluate f()
twice if it yields truthy. Note the possibility of arbitrary behaviour if f()
is not a state-independent function that always returns the same result.
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, which is universally regarded as falsy:
x = f() ?: "default value"
?:
is documented as a distinct operator; [6] this feature was added in Groovy 1.5 [7] (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.?:
binary operator that compares its first operand with null
.return
, like this: valfoo=bar()?:return
?:
operator returns the right operand if the left is null as well.?.
is referred to as the "Elvis operator", [10] but it does not perform the same function. Instead, the null-coalescing operator ??
does.?:
syntax.isNonnull($a) ? $a : $b
. [12] L ?: R
returns the value of L
if it's not nil. Otherwise, return the value of R
. [13] ??
) operator is a logical operator that returns its right-hand side operand when its left-hand side operand is null
or undefined
, and otherwise returns its left-hand side operand. [14] ||
and a similar lower precedence or
[15] . They differ from the bitwise or operator |
which evaluates both operands without short-circuiting. There is also a corresponding assignment operator ||=
that evaluates its right-hand operand and assigns it to the left-operand unless the logical value of the left-operand is true. There is also a short-circuiting defined-or operator //
which evaluates its right-operand and returns its value only if the left-operand is undefined. Finally, the corresponding assignment operator is //=
. Similar exclusive-or operators are not Elvis operators as they do not short-circuit. Other short-circuiting operators are the logical-and ones &&
and and
, but their behavior is opposite that of the Elvis operator.?:
or conditional operator, when used as a ternary operator ??
operator?.
<=>
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.