In certain computer programming 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 of Elvis Presley with his signature hairstyle.[1]
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#[2] or Dart.[3]
The new operator is called Elvis operator because it uses a question mark and a colon together (?:); if you view it sideways, it reminds you of Elvis Presley.
© MMXXIII Rich X Search. We shall prevail. All rights reserved. Rich X Search