Hacker News new | past | comments | ask | show | jobs | submit login

Does the function argument (i.e. the template string) get evaluated regardless of the optional chaining or does it match up with the “roughly equivalent” code?

    log?.(`Request started at ${new Date().toISOString()}`);
    // roughly equivalent to
    //   if (log != null) {
    //       log(`Request started at ${new Date().toISOString()}`);
    //   }



The latter. You can look at what javascript is emitted on the playground.

  let bar: any, log: any;
  log?.(`foo ${bar()}`);
  
  // becomes
  var _a;
  var bar, log;
  (_a = log) === null || _a === void 0 ? void 0 : _a("foo " + bar());


`?` will short-circuit. Nothing is evaluated after that.


Why would it not match up with the example given? They provided it so you'd know the answer to your question.


I copied the example from the article but it says “roughly” so it’s not entirely clear.

The situation that popped in my mind was something like: f?(++a)

Normally you’d expect the side effects incrementation to occur prior to the start of the function invocation. If the function is not evaluated it’s not clear from the article if increment will occur.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: