Elvis operator (original) (raw)

From Wikipedia, the free encyclopedia

Binary operator in computer programming

This article is about the use of a ?: operator as a binary operator. For use as a ternary operator, see ?:.

Elvis Presley, whose hair resembles the operator viewed sideways

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, [? :](/wiki/%3F: "?:"), 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]

Alternative syntaxes

[edit]

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.

Object reference variant

[edit]

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

[edit]

  1. ^ Joyce Farrell (February 7, 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.
  2. ^ "?? Operator". C# Reference. Microsoft. Retrieved December 5, 2018.
  3. ^ "Conditional expressions". Dart Language. Google.
  4. ^ "Using the GNU Compiler Collection (GCC): Conditionals with omitted operands". gcc.gnu.org.
  5. ^ "Using and Porting the GNU Compiler Collection (GCC): C Extensions". gcc.gnu.org.
  6. ^ "Elvis Operator (?: )".
  7. ^ "The Apache Groovy programming language - Groovy 1.5 release notes". groovy-lang.org.
  8. ^ "PHP: Comparison Operators - Manual". PHP website. Retrieved February 17, 2014.
  9. ^ "Null Safety - Kotlin Programming Language". Kotlin.
  10. ^ Albahari, Joseph; Albahari, Ben (2015). C# 6.0 in a Nutshell (6 ed.). O'Reilly Media. p. 59. ISBN 978-1491927069.
  11. ^ Efftinge, Sven. "Xtend - Expressions". eclipse.org.
  12. ^ "Closure Templates - Expressions". GitHub. October 29, 2021.
  13. ^ "Elvis Operator - Ballerina Programming Language". Ballerina. Archived from the original on December 20, 2018. Retrieved December 19, 2018.
  14. ^ "Nullish coalescing operator (??) - JavaScript | MDN". developer.mozilla.org. Retrieved January 5, 2023.
  15. ^ "perlop". Retrieved July 9, 2025.