Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?

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.

Leave a Comment