This article is about the use of a ?: operator as a binary operator. For use as a ternary operator, see ?:.
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]
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"
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.[4] This has been the case since at least GCC 2.95.3 (March 2001), and seems to be the original Elvis operator.[5]
In Apache Groovy, the "Elvis operator" ?: 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.
In PHP, it is possible to leave out the middle part of the ternary operator since PHP 5.3.[8] (June 2009).
The Fantom programming language has the ?: binary operator that compares its first operand with null.
In Kotlin, the Elvis operator returns its left-hand side if it is not null, and its right-hand side otherwise.[9] A common pattern is to use it with return, like this: valfoo=bar()?:return
In Gosu, the ?: operator returns the right operand if the left is null as well.
In Ballerina, the Elvis operator L ?: R returns the value of L if it's not nil. Otherwise, return the value of R.[13]
In JavaScript, the nullish coalescing (??) 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]
^Joyce Farrell (7 February 2013). Java Programming. Cengage Learning. p. 276. ISBN978-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.