ArticleslgStudy

science

Elvis operator

Elvis operator is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Elvis operator rather than just read about it. In short: In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns the first operand if its value is logically true (according to a language-dependent convention, in other words, a truthy value) or returns its second operand if the first operand is not true. That is, it is a short-circuit OR, which returns its first truthy operand.

Elvis operator — main illustration
Elvis operator — illustration

Key takeaways

  • Elvis operator belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Elvis operator to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Elvis operator from memory before moving on to harder problems.

Reference excerpt

In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns the first operand if its value is logically true (according to a language-dependent convention, in other words, a truthy value) or returns its second operand if the first operand is not true. That is, it is a short-circuit OR, which returns its first truthy operand. The syntax 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. It is called the "Elvis operator" because the notation ?:, when viewed sideways, resembles an emoticon of Elvis Presley with his signature hairstyle, the pompadour. 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, 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 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: 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 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, 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. In Perl there is a logical short-circuiting disjunction || and a similar lower precedence or. They differ from the bitwise or operator | which evaluates both operands without short-circuiting. There is also a corresponding assignment operator ||= that evaluates its right-hand operand and assigns it to the left-operand unless the logical value of the left-operand is true. There is also a short-circuiting defined-or operator // which evaluates its right-operand and returns its value only if the left-operand is undefined. Finally, the corresponding assignment operator is //=. Similar exclusive-or operators are not Elvis operators as they do not short-circuit. Other short-circuiting operators are the logical-and ones && and and, but their behavior is opposite that of the Elvis operator.

See also ?: or conditional operator, when used as a ternary operator Null coalescing operator, ?? operator Safe navigation operator, often ?. Spaceship operator <=> Option type

References

Illustrations

Elvis operator: Elvis Presley, whose hair resembles the operator viewed sideways
Elvis Presley, whose hair resembles the operator viewed sideways

Worked examples

Example 1 — a first encounter with Elvis operator

Start with the simplest possible case. Write down what Elvis operator claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Elvis operator before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Elvis operator ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Elvis operator

In research
Elvis operator appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Elvis operator in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Elvis operator is common in secondary-school and first-year university syllabi. It links to neighbouring topics Binary operations, Conditional constructs, Elvis Presley, so understanding it makes those chapters shorter.
In everyday life
Look for Elvis operator outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Elvis operator” →

How to study Elvis operator in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Elvis operator means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Elvis operator out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Elvis operator in simple terms?

In certain computer programming languages, the Elvis operator, often written ?:, is a binary operator that returns the first operand if its value is logically true (according to a language-dependent convention, in other words, a truthy value) or returns its second operand if the first operand is no…

Why does Elvis operator matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Elvis operator?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Elvis operator.

Tags

  • Binary operations
  • Conditional constructs
  • Elvis Presley
  • Operators (programming)

Keep exploring