encyclopegia.org

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.[src] That is, it is a short-circuit OR , which returns its first truthy operand.[src] The syntax of the Elvis operator was inspired by the ternary conditional operator, ?[src] : , since the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional expression A ?[src] A : B .[src] It is called the "Elvis operator" because the notation ?: , when viewed sideways, resembles an emoticon of Elvis Presley with his signature hairstyle.[src] A similar operator is the null coalescing operator, where the boolean truth(iness) check is replaced with a check for non-null instead.[src] This is usually written ??[src] , and can be seen in languages like C# or Dart.[src]

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.[src] That is, it is a short-circuit OR , which returns its first truthy operand.[src] The syntax of the Elvis operator was inspired by the ternary conditional operator, ?[src] : , since the Elvis operator expression A ?: B is approximately equivalent to the ternary conditional expression A ?[src] A : B .[src] It is called the "Elvis operator" because the notation ?: , when viewed sideways, resembles an emoticon of Elvis Presley with his signature hairstyle.[src] A similar operator is the null coalescing operator, where the boolean truth(iness) check is replaced with a check for non-null instead.[src] This is usually written ??[src] , and can be seen in languages like C# or Dart.[src] ==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.[src] It is equivalent to this example, using the conditional ternary operator: x = f() ?[src] f() : g() except that it does not evaluate f() twice if it yields truthy.[src] Note the possibility of arbitrary behaviour if f() is not a state-independent function that always returns the same result.[src] ===Object reference variant=== This code will result in a reference to an object that is guaranteed to not be null.[src] 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.[src] This has been the case since at least GCC 2.95.3 (March 2001), and seems to be the original Elvis operator.[src] In Apache Groovy, the "Elvis operator" ?: is documented as a distinct operator; this feature was added in Groovy 1.5 (December 2007).[src] 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.[src] In PHP, it is possible to leave out the middle part of the ternary operator since PHP 5.3.[src] (June 2009).[src] The Fantom programming language has the ?: binary operator that compares its first operand with null .[src] In Kotlin, the Elvis operator returns its left-hand side if it is not null, and its right-hand side otherwise.[src] 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.[src] In C#, the null-conditional operator, ?.[src] is referred to as the "Elvis operator", but it does not perform the same function.[src] Instead, the null-coalescing operator ??[src] does.[src] In ColdFusion and CFML, the Elvis operator was introduced using the ?: syntax.[src] The Xtend programming language has an Elvis operator.[src] In Google's Closure Templates, the Elvis operator is a null coalescing operator, equivalent to isNonnull($a) ?[src] $a : $b .[src] In Ballerina, the Elvis operator L ?: R returns the value of L if it's not nil.[src] Otherwise, return the value of R .[src] In JavaScript, the nullish coalescing ( ??[src] ) 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.[src] In Perl there is a logical short-circuiting disjunction || and a similar lower precedence or .[src] They differ from the bitwise or operator | which evaluates both operands without short-circuiting.[src] 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.[src] 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.[src] Finally, the corresponding assignment operator is //= .[src] Similar exclusive-or operators are not Elvis operators as they do not short-circuit.[src] Other short-circuiting operators are the logical-and ones && and and , but their behavior is opposite that of the Elvis operator.[src] ==See also== ?: or conditional operator, when used as a ternary operator Null coalescing operator, operator Safe navigation operator, often ?.[src] Spaceship operator <=> Option type ==References==[src]

53 sentences · 53 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Elvis operator." encyclopegia.org, 2026, https://encyclopegia.org/wiki/elvis-operator. Accessed 28 November 2026.

Chicago

"Elvis operator." encyclopegia.org. Accessed November 28, 2026. https://encyclopegia.org/wiki/elvis-operator.

APA

encyclopegia.org. (2026). Elvis operator. Retrieved November 28, 2026, from https://encyclopegia.org/wiki/elvis-operator

Accessed November 28, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1377237894 · 0.5 · snapshot enwiki-001377237894

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools
  • Careers — open roles: frontier engineers and sales partners
  • Index — every corpus, paper and record
  • Estate guide — what a public data estate is
  • Founder — Lois-Kleinner Alpasan
  • Records — DOIs and external deposits
  • Documents — one-pagers, PDFs and QR codes
  • Brand kit — logos and share images
  • Papers — plain-language briefs with citations
  • Browser — search engine, PWA and bookmarklet
  • Anticloud — the operator and its projects

As seen in Business Insider · 336 outlets · evidence DOIs · plain-language papers

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.