Elvis operator

In certain computer programming languages, the Elvis operator, often written  , is a binary operator that returns the evaluated first operand if that operand evaluates to a value likened to logically true (according to a language-dependent convention, in other words, a truthy value), and otherwise returns the evaluated second operand (in which case the first operand evaluated to a value likened to logically false, in other words, a falsy value). 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   is approximately equivalent to the ternary conditional expression.

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.

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# or Dart.

Alternative syntaxes
In several languages, such as Common Lisp, Clojure, Lua, Object Pascal, Perl, Python, Ruby, and JavaScript, the OR operator (typically  or  ) has the same behavior as the above: returning its first operand if it would evaluate to a truthy value, and otherwise evaluating and returning its second operand, which may be a truthy or falsy value. When the left-hand side is truthy, 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 (proper) boolean.

Boolean variant
In a language that supports the Elvis operator, something like this:

will set  equal to the result of   if that result is truthy, and to the result of   otherwise.

It is equivalent to this example, using the conditional ternary operator:

except that it does not evaluate  twice if it yields truthy. Note the possibility of arbitrary behaviour if  is not a state-independent function that always returns the same result.

Object reference variant
This code will result in a reference to an object that is guaranteed to not be null. Function  returns an object reference instead of a boolean, and may return null, which is universally regarded as falsy:

Languages supporting the Elvis operator

 * Perl since version v5.10 provides the Logical Defined Or operator:, equivalent to
 * 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, 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   does.
 * In ColdFusion and CFML, the Elvis operator was introduced using the  syntax.
 * The Xtend programming language has an Elvis operator.
 * In Google's Closure Templates, the Elvis operator is a null coalescing operator, equivalent to.
 * In Ballerina, the Elvis operator  returns the value of   if it's not nil. Otherwise, return the value of.
 * In JavaScript, the nullish coalescing operator is a logical operator that returns its right-hand side operand when its left-hand side operand is   or , and otherwise returns its left-hand side operand.