Elvis Operator¶
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.
Core Idea¶
The Elvis operator, often written ?:, is a binary operator found in certain programming languages that returns its first operand if that operand's value is logically true, and otherwise returns its second operand. What counts as "true" follows a language-dependent convention, so the test is for a truthy value rather than strictly a Boolean true. In effect the operator is a short-circuit OR that yields its first truthy operand.
Its syntax was inspired by the ternary conditional operator ? :: the expression A ?: B is approximately equivalent to A ? A : B, except that A is evaluated only once. That difference matters when A is a function call whose result is not state-independent, since evaluating it twice could give different results. In GNU C and C++, the Elvis form arises because the middle operand of the ternary operator is optional, while Groovy requires the binary ?: to be written as a single operator with no whitespace.
The name comes from the notation itself: viewed sideways, ?: resembles an emoticon of Elvis Presley with his signature hairstyle. The nearest relative is the null coalescing operator, usually written ?? (as in C# or Dart), which replaces the truthiness check with a check for non-null. In languages such as Python, Perl, Ruby, Lua and JavaScript, the ordinary logical OR already short-circuits and returns its first truthy operand, so no separate Elvis operator is needed.
How would you explain it like I'm…
The Backup Plan Rule
Use This, Or Else That
First-Truthy Default Operator
Structural Signature¶
Sig role-phrases:
- Defining carrier — 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 ?
- Constitutive relation — will set x equal to the result of f() if that result is truthy, and to the result of g() otherwise.
- Operating condition — except that it does not evaluate f() twice if it yields truthy.
- Recognition evidence — Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result.
- Admissible variation — This code will result in a reference to an object that is guaranteed to not be null.
- Characteristic consequence — Function f() returns an object reference instead of a boolean, and may return null, which is universally regarded as falsy.
- Failure boundary — In GNU C and C++ (that is: in C and C++ with GCC extensions), the second operand of the ternary operator is optional.
What It Is Not¶
- Not the whole field of mathematics, logic, and statistics. The node requires the specific identity stated by 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.
- Not an over-broad reading. except that it does not evaluate f() twice if it yields truthy.
- Not an over-broad reading. 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.
- Not an over-broad reading. Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result.
- Not automatically Formula Calculator. Retrieval proximity does not establish equivalence; the two identities must be compared by carrier, operation, and failure boundary.
Scope of Application¶
Elvis Operator applies literally inside mathematics, logic, and statistics wherever the source-defined carrier and relation can be established. Its documented habitats include:
- ExampleBoolean variant. Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result.
- Object reference variant. Function f() returns an object reference instead of a boolean, and may return null, which is universally regarded as falsy.
- Languages supporting the Elvis operator. In C#, the null-conditional operator, ?. is referred to as the "Elvis operator", but it does not perform the same function.
- ExampleBoolean variant. will set x equal to the result of f() if that result is truthy, and to the result of g() otherwise.
- ExampleBoolean variant. except that it does not evaluate f() twice if it yields truthy.
- Object reference variant. This code will result in a reference to an object that is guaranteed to not be null.
Outside mathematics, logic, and statistics, the name should be retained only when these same operational conditions survive; otherwise the comparison belongs to the broader parent Pattern or should be marked as analogy.
Clarity¶
A clear use of Elvis Operator names the carrier, the operative relation, and the conditions under which the source treats the identity as present. The minimal definition is 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. The strongest recognition evidence is: Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result. A report should distinguish that evidence from a proxy, consequence, or common implementation. It should also state the qualification except that it does not evaluate f() twice if it yields truthy. so that a reader can reproduce the classification rather than infer it from topical resemblance.
Manages Complexity¶
Elvis Operator compresses multiple mathematics, logic, and statistics details into a stable diagnostic relation. The source shows both the central mechanism—will set x equal to the result of f() if that result is truthy, and to the result of g() otherwise.—and the practical consequence—function f() returns an object reference instead of a boolean, and may return null, which is universally regarded as falsy. This compression makes cases comparable while leaving parameters, conventions, exceptions, and evidential quality explicit. It is lossy by design: local history and implementation details may be omitted only when they do not alter the defining relation.
Abstract Reasoning¶
- Type the carrier. Identify the mathematics, logic, and statistics entities to which the claim applies.
- State the relation. Use the source-grounded identity: 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.
- Check operation and conditions. except that it does not evaluate f() twice if it yields truthy.
- Demand recognition evidence. Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result.
- Test variation. Change an implementation or setting while preserving this code will result in a reference to an object that is guaranteed to not be null.
- Run the collapse test. Remove the defining operation; if the label still seems equally apt, only a topic or correlate was retained.
- Reduce cautiously. When the specialist conditions cannot be carried, route the residual comparison to Pattern.
Knowledge Transfer¶
Within the home domain. Knowledge about Elvis Operator transfers literally when a new case preserves the same carrier type, relation, and recognition test. Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result. Function f() returns an object reference instead of a boolean, and may return null, which is universally regarded as falsy.
Beyond the home domain. No canonical parent is asserted for Elvis Operator. An outside case receives the specialist name only when the same typed roles and rejection conditions can be filled literally; otherwise the comparison remains an analogy pending later graph densification.
Examples¶
Canonical¶
This has been the case since at least GCC 2.95.3 (March 2001), and seems to be the original Elvis operator. This case is canonical because it supplies a concrete carrier and lets the defining relation be checked rather than merely named.
Mapped back: carrier → the entities in the documented case; operation → 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; recognition evidence → Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result
Applied / In Practice¶
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++). The applied case shows how the identity is used under a second setting or qualification while keeping the same operative relation.
Mapped back: changed setting → Alternative syntaxes; invariant → 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; boundary → the case exits the class when except that it does not evaluate f() twice if it yields truthy
Structural Tensions¶
T1 — Stable identity versus admissible variation. except that it does not evaluate f() twice if it yields truthy. The tension matters because emphasizing only one side either dissolves the identity or overstates what the evidence and domain conventions warrant.
Diagnostic: Which changes preserve the defining relation, and which replace it?
T2 — Recognition versus proxy. 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. The tension matters because emphasizing only one side either dissolves the identity or overstates what the evidence and domain conventions warrant.
Diagnostic: Does the cited evidence establish the identity or only a correlated sign?
T3 — Definition versus implementation. Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result. The tension matters because emphasizing only one side either dissolves the identity or overstates what the evidence and domain conventions warrant.
Diagnostic: Is the observed implementation constitutive, optional, or merely common?
T4 — Scope versus overextension. This code will result in a reference to an object that is guaranteed to not be null. The tension matters because emphasizing only one side either dissolves the identity or overstates what the evidence and domain conventions warrant.
Diagnostic: Can every claimed application fill the same typed roles without metaphor?
T5 — Transfer versus domain accent. 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 ? The tension matters because emphasizing only one side either dissolves the identity or overstates what the evidence and domain conventions warrant.
Diagnostic: Does the receiving case instantiate Elvis Operator literally, co-instantiate Pattern, or only resemble it?
T6 — Autonomy versus reduction. will set x equal to the result of f() if that result is truthy, and to the result of g() otherwise. The tension matters because emphasizing only one side either dissolves the identity or overstates what the evidence and domain conventions warrant.
Diagnostic: What does Elvis Operator distinguish that the broader parent Pattern leaves together?
Structural–Framed Character¶
Elvis Operator is structural-leaning. Its structural side is the repeatable organization summarized by 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. Its framed side is the mathematics, logic, and statistics vocabulary that fixes the carrier, evidence, exceptions, and admissible transformations.
Evaluative weight: the identity can be stated descriptively even when applications carry practical stakes. Human-practice dependence: the source-grounded carrier determines whether the relation exists independently or is constituted by a practice. Institutional origin: disciplinary conventions stabilize the name and test. Vocabulary portability: except that it does not evaluate f() twice if it yields truthy. Import versus recognition: literal transfer requires the same mechanism; shape alone is analogy.
Its portable skeleton is Pattern. Its character: a recurring specialist identity whose thin organization can be abstracted, while its operational meaning remains domain-bound.
Structural Core vs. Domain Accent¶
What is skeletal. 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. The stable skeleton is the typed relation expressed in that definition and the entry's recognition and collapse tests. The source identifies these operative conditions: 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 ? will set x equal to the result of f() if that result is truthy, and to the result of g() otherwise. It further constrains recognition and variation through: 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.
What is domain-bound. mathematics, logic, and statistics supplies the operative entities, technical vocabulary, warrants, and exceptions that make Elvis Operator literal. Its documented scope includes the condition that Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result. Another bounded application condition is that Function f() returns an object reference instead of a boolean, and may return null, which is universally regarded as falsy. These are not decorative examples; they determine which carrier and evidence can fill the abstraction's roles.
Why no parent is asserted. Removing those specialist details does not currently yield one live catalog node that is a necessary genus for every instance. The entry is therefore approved as unparented rather than attached by topical resemblance. Its collapse evidence remains specific—This code will result in a reference to an object that is guaranteed to not be null.—and future graph densification may discover a defensible relation only if it preserves that boundary.
Instantiates / Related Primes¶
This entry under conditions is a kind of Logical Operation.
- Approved unparented node. No current live node supplies a defensible necessary genus or structural prerequisite for Elvis Operator. The reviewed identity is: 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. The accelerated suggestion was declined because topical or lexical similarity does not establish hierarchy; the node is admitted without a parent pending later graph densification.
- Related reasoning operations. Evidence, representation, comparison, classification, transformation, or evaluation may participate in particular cases, but participation does not make any one of them a necessary parent of every instance.
Relationships to Other Abstractions¶
Current abstraction Elvis Operator Domain-specific
Parents (1) — outgoing typed relations
-
Elvis Operator is a kind of, conditional Logical Operation Domain-specific
Supported where the programming language formally defines truthiness-based selection; it is also a programming-language operator.Supported where the programming language formally defines truthiness-based selection; it is also a programming-language operator.
Condition / exception Supported where the programming language formally defines truthiness-based selection; it is also a programming-language operator.
Hierarchy path (1) — routes to 1 parentless root
- Elvis Operator → is a kind of, conditional Logical Operation → is a kind of Transformation → is a kind of, typical Function (Mapping)
Neighborhood in Abstraction Space¶
Elvis Operator sits in a moderately populated region (44th percentile for distinctiveness): it has near-neighbors but no dense thicket of look-alikes.
Family — Formal Logic & Language Constructs (20 abstractions)
Nearest neighbors
- Filling radius — 0.86
- Julia set — 0.86
- False position method — 0.86
- Big O in probability notation — 0.86
- Typographical Number Theory — 0.86
Computed from structural-signature embeddings · 2026-11-14
Not to Be Confused With¶
- Pattern. The parent omits the specialist differentia. Tell: Can the case establish 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?
- Formula Calculator. Capture a complete mathematical expression as an editable representation, then parse and evaluate it under declared grammar, precedence, associativity, arity, and numeric-context rules when the user commits the calculation. Tell: Which entry's carrier, operation, and failure condition are satisfied?
- Relational operator. A programming-language operator that compares two operands under a specified relation and returns a truth-valued result. Tell: Which entry's carrier, operation, and failure condition are satisfied?
- Logical connective. A formal operator that forms a compound formula from one or more formulas and whose semantics determines the compound's truth or satisfaction from its arguments. Tell: Which entry's carrier, operation, and failure condition are satisfied?
- A measurement, proxy, or consequence. Those may provide evidence without being the identity. Tell: Would Elvis Operator remain present if the detector or downstream effect changed?
- A metaphorical analogue. A similar shape outside mathematics, logic, and statistics lacks the specialist mechanism. Tell: Do the native roles transfer literally, or only the parent Pattern?
References¶
- Frozen Wikipedia discovery revision: https://en.wikipedia.org/wiki/Elvis_operator (revision 1352624481).