Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional Chaining in JavaScript [duplicate]

Tags:

I've been programming a lot in Swift recently. Today I did some work in JavaScipt when question popped up to me:

Is there something similar to optional chaining in JavaScript? A way to prevent undefined is not an object without any variables?

Example:

function test(){
   if(new Date() % 2){
      return {value: function(){/*code*/}};
   }
} 

test().value();

will fail half of time because sometimes test returns undefined.

The only solution I can think of is a function:

function oc(object, key){
   if(object){
      return object[key]();
   }
}

oc(test(), 'value');

I would like to be able to do something like:

test()?.value()

The part after the question mark is only executed if test returned an object.

But this is not very elegeant. Is there something better? A magic combination of operators?

Edit I know I could rewrite test to return something. But I'm wondering if there's something like optional chaining. I'm not interested in a particular solution to the above example. Something that I also can use if have no control over the function returning undefined.

like image 932
idmean Avatar asked Oct 03 '14 17:10

idmean


People also ask

Can I use optional chaining in JS?

You can use optional chaining when attempting to call a method which may not exist. This can be helpful, for example, when using an API in which a method might be unavailable, either due to the age of the implementation or because of a feature which isn't available on the user's device.

What is optional chaining in JavaScript?

Introduction to the JavaScript optional chaining operator The optional chaining operator ( ?. ) allows you to access the value of a property located deep within a chain of objects without explicitly checking if each reference in the chain is null or undefined .

Does optional chaining return null or undefined?

Optional chaining is a safe and concise way to perform access checks for nested object properties. The optional chaining operator ?. takes the reference to its left and checks if it is undefined or null. If the reference is either of these nullish values, the checks will stop and return undefined.

What is optional chaining and explain with syntax?

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil , the property, method, or subscript call returns nil .


2 Answers

This is currently a Stage 4 proposal you can check on the progress of it here:
https://github.com/tc39/proposal-optional-chaining

You can use the babel plugin today:
https://www.npmjs.com/package/babel-plugin-transform-optional-chaining

Update 11th January 2020: Babel now supports optional chaining by default https://babeljs.io/blog/2020/01/11/7.8.0

The Optional Chaining operator is spelled ?.. It may appear in three positions:

obj?.prop       // optional static property access
obj?.[expr]     // optional dynamic property access
func?.(...args) // optional function or method call

Notes:

In order to allow foo?.3:0 to be parsed as foo ? .3 : 0 (as required for backward compatibility), a simple lookahead is added at the level of the lexical grammar, so that the sequence of characters ?. is not interpreted as a single token in that situation (the ?. token must not be immediately followed by a decimal digit).

Also worth checking out:

https://github.com/tc39/proposal-nullish-coalescing

https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-nullish-coalescing-operator

like image 123
Jonathan Wood Avatar answered Sep 20 '22 17:09

Jonathan Wood


In plain JavaScript you have to do type checks or structure your code so that you know an object will exist.

CoffeeScript, a language that compiles down to JavaScript, provides an existential operator ?. for safe chaining if you're willing to consider a preprocessed language.

There's another discussion here about why you can't reproduce this behavior in JS.

There is also a discussion on the ESDiscuss forums about adding an existential operator to a future version of JavaScript. It doesn't seem very far along though, certainly nowhere close to practical use. More of an idea at this point.

like image 34
Ben McCormick Avatar answered Sep 22 '22 17:09

Ben McCormick