
In certain
computer programming
Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
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
An emoticon (, , rarely , ), short for emotion icon, is a pictorial representation of a facial expression using Character (symbol), characters—usually punctuation marks, numbers and Alphabet, letters—to express a person's feelings, mood ...
of
Elvis Presley
Elvis Aaron Presley (January 8, 1935 – August 16, 1977) was an American singer and actor. Referred to as the "King of Rock and Roll", he is regarded as Cultural impact of Elvis Presley, one of the most significant cultural figures of the ...
with his
signature hairstyle.
A similar operator is the
null coalescing operator
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since versi ...
, where the boolean truth(iness) check is replaced with a check for non-
null
Null may refer to:
Science, technology, and mathematics Astronomy
*Nuller, an optical tool using interferometry to block certain sources of light Computing
*Null (SQL) (or NULL), a special marker and keyword in SQL indicating that a data value do ...
instead. This is usually written
??
, and can be seen in languages like
C# or
Dart.
Alternative syntaxes
In several languages, such as
Common Lisp
Common Lisp (CL) is a dialect of the Lisp programming language, published in American National Standards Institute (ANSI) standard document ''ANSI INCITS 226-1994 (S2018)'' (formerly ''X3.226-1994 (R1999)''). The Common Lisp HyperSpec, a hyperli ...
,
Clojure
Clojure (, like ''closure'') is a dynamic programming language, dynamic and functional programming, functional dialect (computing), dialect of the programming language Lisp (programming language), Lisp on the Java (software platform), Java platfo ...
,
Lua,
Object Pascal
Object Pascal is an extension to the programming language Pascal (programming language), Pascal that provides object-oriented programming (OOP) features such as Class (computer programming), classes and Method (computer programming), methods.
T ...
,
Python,
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
, and
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
, 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.
Example
Boolean variant
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
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. 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
Fantom is a Swedish velomobile with four wheels, two in the front and two in the rear. It has no front suspension, but has suspension in the rear.
Fantom was never sold as a finished product. Instead it was sold as a set of drawings. The drawin ...
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. A common pattern is to use it with
return
, like this:
* 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
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since versi ...
, equivalent to
isNonnull($a) ? $a : $b
.
* In
Ballerina, the Elvis operator
L ?: R
returns the value of
L
if it's not nil. Otherwise, return the value of
R
.
* In
JavaScript
JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior.
Web browsers have ...
, 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.
See also
*
?:
or
conditional operator, when used as a
ternary operator
*
Null coalescing operator
The null coalescing operator is a binary operator that is part of the syntax for a basic conditional expression in several programming languages, such as (in alphabetical order): C# since version 2.0, Dart since version 1.12.0, PHP since versi ...
, operator
*
Safe navigation operator, often
?.
*
Spaceship operator <=>
*
Option type
References
{{reflist
Articles with example code
Binary operations
Conditional constructs
Operators (programming)
Elvis Presley