You can use the logical 'OR' operator in place of the Elvis operator:

For example displayname = user.name || "Anonymous" .

But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.

For example The Existential Operator

zip = lottery.drawWinner?().address?.zipcode

Function shortcuts

()->  // equivalent to function(){}

Sexy function calling

func 'arg1','arg2' // equivalent to func('arg1','arg2')

There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.

Answer from Lime on Stack Overflow

You can use the logical 'OR' operator in place of the Elvis operator:

For example displayname = user.name || "Anonymous" .

But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.

For example The Existential Operator

zip = lottery.drawWinner?().address?.zipcode

Function shortcuts

()->  // equivalent to function(){}

Sexy function calling

func 'arg1','arg2' // equivalent to func('arg1','arg2')

There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.

Answer from Lime on Stack Overflow
Top answer
1 of 16
176

You can use the logical 'OR' operator in place of the Elvis operator:

For example displayname = user.name || "Anonymous" .

But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.

For example The Existential Operator

zip = lottery.drawWinner?().address?.zipcode

Function shortcuts

()->  // equivalent to function(){}

Sexy function calling

func 'arg1','arg2' // equivalent to func('arg1','arg2')

There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.

2 of 16
155

2020 Update

JavaScript now has equivalents for both the Elvis Operator and the Safe Navigation Operator.


Safe Property Access

The optional chaining operator (?.) is currently a stage 4 ECMAScript proposal. You can use it today with Babel.

// `undefined` if either `a` or `b` are `null`/`undefined`. `a.b.c` otherwise.
const myVariable = a?.b?.c;

The logical AND operator (&&) is the "old", more-verbose way to handle this scenario.

const myVariable = a && a.b && a.b.c;

Providing a Default

The nullish coalescing operator (??) is currently a stage 4 ECMAScript proposal. You can use it today with Babel. It allows you to set a default value if the left-hand side of the operator is a nullary value (null/undefined).

const myVariable = a?.b?.c ?? 'Some other value';

// Evaluates to 'Some other value'
const myVariable2 = null ?? 'Some other value';

// Evaluates to ''
const myVariable3 = '' ?? 'Some other value';

The logical OR operator (||) is an alternative solution with slightly different behavior. It allows you to set a default value if the left-hand side of the operator is falsy. Note that the result of myVariable3 below differs from myVariable3 above.

const myVariable = a?.b?.c || 'Some other value';

// Evaluates to 'Some other value'
const myVariable2 = null || 'Some other value';

// Evaluates to 'Some other value'
const myVariable3 = '' || 'Some other value';
🌐
Wikipedia
en.wikipedia.org › wiki › Elvis_operator
Elvis operator - Wikipedia
2 weeks ago - In Ballerina, the Elvis operator ... nullish coalescing (??) 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....
🌐
Medium
medium.com › @under_the_hook › javascript-the-logical-operators-ternary-elvis-safe-navigation-4a5ab2e2c7cf
🎸 JavaScript — the Logical Operators (Ternary | Elvis | Safe navigation) | by #Under_The_Hook | Medium
December 12, 2022 - The ternary operator has been part of the JavaScript language since it was first introduced. The operator takes 3 operands and places them in this format: ... The bullish coalescing operator also known as the “Elvis operator”, was landed ...
🌐
Beyondjava
beyondjava.net › elvis-operator-aka-safe-navigation-javascript-typescript
Elvis Operator (aka Safe Navigation) in JavaScript and TypeScript
In most languages implementing the Elvis operator, the expression null?.firstName simply returns null, relieving the developers of guarding the attribute accesses with countless if statements. Languages like JavaScript are even worse than the average programming languages.
🌐
Delft Stack
delftstack.com › home › howto › javascript › javascript elvis operator
The Elvis Operator in JavaScript | Delft Stack
February 2, 2024 - Python 3 Basic Tkinter Python Modules ...MySQLMongoDBPostgresSQLiteRVBAScalaRaspberry Pi ... The Elvis Operator is a shorter version of the ternary operator....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Optional_chaining
Optional chaining (?.) - JavaScript | MDN
By using the ?. operator instead of just ., JavaScript knows to implicitly check to be sure obj.first is not null or undefined before attempting to access obj.first.second.
Find elsewhere
🌐
GitHub
gist.github.com › cdmckay › 6ac36defdc01d39080fa
A simple "elvis" operator in JavaScript · GitHub
A simple "elvis" operator in JavaScript. GitHub Gist: instantly share code, notes, and snippets.
🌐
Naresha's Blog
blog.nareshak.com › groovy-elvis-operator
Groovy Elvis Operator ?:
March 14, 2025 - JavaScript has this complexity of null and undefined. Hence, the nullish coalesce operator makes sense there. While in Groovy, we only have null. Therefore, if we want to check for null value instead of the boolean coerced value, we could explicitly use the expression input != null for example. Since the purpose of the Elvis operator is mostly to assign a default value, Groovy, in its version 3, introduced the Elvis assignment operator.
🌐
LambdaTest Community
community.lambdatest.com › general discussions
Is there a direct implementation of the elvis operator Javascript? - LambdaTest Community
October 9, 2024 - Is there an elvis operator javascript equivalent for the null-coalescing operator or safe navigation operator? I’ll explain with examples: Elvis Operator (?:) The “Elvis operator” is a shorthand version of Java’s ternary operator. It’s handy for returning a ‘sensible default’ value if an expression resolves to false or null.
🌐
Lucee Dev
dev.lucee.org › support
Elvis operator - support - Lucee Dev
July 25, 2023 - Hi all is there a better way to ... rc.keyExists('areaId') ?? params.append( { 'areaId': rc.areaId } ) Removing the left expression, if the first condition ......
🌐
Vercel
bekk-christmas.vercel.app › post › 2018 › 03 › elvis-operator
Elvis Operator
As you learned on Day 1 JavaScript is always open for suggested features and additions. One of these suggestions is called "Optional Chaining", and is about the operator best known as the Elvis operator.
🌐
Reddit
reddit.com › r/programmertil › til about the nullish coalescing operator in javascript (??)
r/ProgrammerTIL on Reddit: TIL about the Nullish Coalescing Operator in Javascript (??)
March 30, 2022 -

Often if you want to say, "x, if it exists, or y", you'd use the or operator ||.

Example:

const foo = bar || 3;

However, let's say you want to check that the value of foo exists. If foo is 0, then it would evaluate to false, and the above code doesn't work.

So instead, you can use the Nullish Coalescing Operator.

const foo = bar ?? 3;

This would evaluate to 3 if bar is undefined or null, and use the value of bar otherwise.

In typescript, this is useful for setting default values from a nullable object.

setFoo(foo: Foo|null) {
  this.foo = foo ?? DEFAULT_FOO;
}
🌐
GitHub
github.com › ligershark › WebOptimizer › issues › 169
Parsing the "Elvis Operator" (optional chaining like '?.") throws null reference exception · Issue #169 · ligershark/WebOptimizer
November 6, 2020 - The optional chaining operator ?. causes a request to a JavaScript file containing it to return a 500, with the exception: NullReferenceException: Object reference not set to an instance of an obje...
Published   May 18, 2021
🌐
SamanthaMing
samanthaming.com › tidbits › 52-3-ways-to-set-default-value
3 Ways to Set Default Value in JavaScript | SamanthaMing.com
The && is also known as the Guard Operator. So here's a quick summary of the difference: ||: 1st expression is always outputted. The 2nd expression only gets outputted if the 1st expression is falsy. &&: 1st expression is outputted if it's FALSY. The 2nd expression only get outputted if the 1st expression is truthy. This is an interesting one. In JavaScript we have || to set default values. In other languages such as C++, this behavior is similar to the Elvis Operator which is denoted as ?:.
🌐
underoot
underoot.dev › blog › 2023 › 06 › 22 › elvis-operator
Elvis Operator
Someone also referring to another ... one in situations like this: ... So, basically, nope. JavaScript doesn't have a true Elvis operator but has a coalescing version of it....
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Conditional_operator
Conditional (ternary) operator - JavaScript | MDN
The ternary operator is right-associative, which means it can be "chained" in the following way, similar to an if … else if … else if …
🌐
javascriptroom
javascriptroom.com › blog › replacement-of-elvis-operator-of-angular2-in-typescript
TypeScript's Alternative to Angular2 Elvis Operator: Using Optional Chaining for Safe Nested Property Access
If you’ve worked with Angular 2+ (now simply "Angular") or JavaScript/TypeScript, you’ve likely encountered the frustration of accessing nested object properties. A common pitfall is the `Cannot read property 'x' of undefined` error, which occurs when a parent property in the chain is `null` or `undefined`. In early Angular versions, developers often relied on the "Elvis Operator" (`?.`) in templates to safely access nested properties.