DEV Community

Discussion on: Prevent Object Retrieval TypeError with &&

Collapse
 
ggenya132 profile image
Eugene Vedensky

That spicy proposal looks exciting. 'Elvis' operator is totally suited to javascript

Collapse
 
samanthaming profile image
Samantha Ming

Hahaha, I think I learned something new! Never knew that was called the ‘elvis’ operator 😂

Collapse
 
ggenya132 profile image
Eugene Vedensky

I first heard it referred to as that when I was reading through a groovy scripting tutorial!

Thread Thread
 
scheidig profile image
Albrecht Scheidig • Edited

The Elvis operator ?: returns left-hand side if it is trueish, right-hand side otherwise, thus comparable to object || default. groovy-lang.org/operators.html#_el...
It is called Elvis because it looks like an Elvis emoticon, but I always memoized it with "Not sure if he still exists" :-)

  king?:sing()

OTOH, the safe-navigation operator ?. will return left-hand side if it is null, otherwise evaluate right-hand side on the result of left-hand side, thus comparable to object && object.prop groovy-lang.org/operators.html#_sa...

So the proposal for JavaScript is not the Elvis operator, but comparable to the safe-navigation operator.

Thread Thread
 
samanthaming profile image
Samantha Ming

Indeed! Yes, thanks for the correction. In my new blog post, I talk about the Elvis Operator and hopefully others won't make the same confusion as I had 😅