Skip to main content

Elvis Operator

Elvis operator exists in many computer languages. It allows you to specify a fallback value in case a variable is empty. For example in Kotlin you can do the following:

fun getNameLength(name: String?) {
return name?.length ?: 0
}

The origin one #

What language did the operator get first? According to the Wikipedia page, it was GNU C/C++. You maybe know about the ternary operator. For example in the following code fragment on C++ you can choose b if a does have some empty value like 0 or '' or c otherwise:

auto d = a ? b : c;

And the first operand is optional and can be easily omitted:

auto d = a ? : c; // or even
auto d = a ?: c;

It will be equivalent to the next one:

auto d = a ? a : c;

But why the Elvis operator is called so? Answer that ligature of the operator is like the very famous quiff of Rock'n'Roll star — Elvis Presley:

There is the same emoticon, which was an inspiration for the authors of this name.

Does the Elvis operator exist in JavaScript? #

When with colleagues we discussed the proposal of this operator when it was only in the 3rd stage we often referred to it as Elvis operator. But syntactically it's wrong because the ligature of nullish coalescing in JavaScript is ?? — double quiff without eyes. Someone also referring to another proposal, optional chaining, as the Elvis operator, which is also incorrect. The last one is compound for the former one in situations like this:

let length = name?.length ?? 0;

So, basically, nope. JavaScript doesn't have a true Elvis operator but has a coalescing version of it.