In several languages, such as Common Lisp, Clojure, Lua, Object Pascal, 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:
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:
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.
Main article: null coalescing operator
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:
Joyce Farrell (7 February 2013). Java Programming. Cengage Learning. 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. 978-1285081953 ↩
"?? Operator". C# Reference. Microsoft. Retrieved 5 December 2018. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/null-coalescing-operator ↩
"Conditional expressions". Dart Language. Google. https://dart.dev/language/operators#conditional-expressions ↩
"Using the GNU Compiler Collection (GCC): Conditionals with omitted operands". gcc.gnu.org. https://gcc.gnu.org/onlinedocs/gcc/extensions-to-the-c-language-family/conditionals-with-omitted-operands.html ↩
"Using and Porting the GNU Compiler Collection (GCC): C Extensions". gcc.gnu.org. https://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC70 ↩
"Elvis Operator (?: )". http://docs.groovy-lang.org/latest/html/documentation/index.html#_elvis_operator ↩
"The Apache Groovy programming language - Groovy 1.5 release notes". groovy-lang.org. http://groovy-lang.org/releasenotes/groovy-1.5.html ↩
"PHP: Comparison Operators - Manual". PHP website. Retrieved 2014-02-17. http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary ↩
"Null Safety - Kotlin Programming Language". Kotlin. https://kotlinlang.org/docs/reference/null-safety.html#elvis-operator ↩
Albahari, Joseph; Albahari, Ben (2015). C# 6.0 in a Nutshell (6 ed.). O'Reilly Media. p. 59. ISBN 978-1491927069. 978-1491927069 ↩
Efftinge, Sven. "Xtend - Expressions". eclipse.org. https://eclipse.org/xtend/documentation/203_xtend_expressions.html#elvis-operator ↩
"Closure Templates - Expressions". GitHub. 29 October 2021. https://github.com/google/closure-templates/blob/master/documentation/reference/expressions.md#null-coalescing-operator--null-coalescing-operator ↩
"Elvis Operator - Ballerina Programming Language". Ballerina. Archived from the original on 2018-12-20. Retrieved 2018-12-19. https://web.archive.org/web/20181220230754/https://ballerina.io/learn/by-example/elvis-operator.html ↩
"Nullish coalescing operator (??) - JavaScript | MDN". developer.mozilla.org. Retrieved 2023-01-05. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing ↩