1

today i saw a JS code with a strange syntaxe for me : "?.", for example in :

function getSortDirection({columnName}) {
    return sortings?.[0].sortBy === columnName ? sortings?.[0].isSortDescending ? 'desc' : 'asc' : "none";
  }

the sortings[0].sortBy is trivial but sortings?.[0].sortBy not.

Any explanation are welcomed

Gabriele Petrioli
  • 173,972
  • 30
  • 239
  • 291
AlainIb
  • 3,925
  • 3
  • 28
  • 52
  • 7
    It's for [optional chaining](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#optional-chaining). – zero298 Feb 13 '20 at 22:09
  • [Also relevant](https://stackoverflow.com/questions/32139078/null-safe-property-access-and-conditional-assignment-in-es6-2015) – VLAZ Feb 13 '20 at 22:10
  • thanks guy. it's a cool feature – AlainIb Feb 13 '20 at 22:11
  • 1
    It is specific to Babel, that code needs to be compiled in order to run. – Travis J Feb 13 '20 at 22:11
  • 2
    @TravisJ This is not specific to Babel. It's an experimental TC39 proposal that Babel supports. This is also available in TypeScript. –  Feb 13 '20 at 22:13
  • Thanks for the clarification, it was my understanding that only compiled JavaScript was using this. More and more c# features coming to JavaScript it seems. Maybe we can get some strong typing ;) – Travis J Feb 13 '20 at 22:16
  • @TravisJ there is TypeScript... – VLAZ Feb 13 '20 at 22:25
  • @TravisJ *transpiled*, not *compiled* - both Babel and TypeScript are transpilers, hence how they can support such features before they're actually added in the language, since they simply convert sugary syntax in JS source to its equivalent long-form source code using existing supported language features. – Klaycon Feb 13 '20 at 22:30
  • @Klaycon - "Babel · The compiler for next generation JavaScript", just using their verbiage. At the point where you have progressed the abstraction into another language, it isn't entirely improper to call it compiling. As well, Babel and Typescript are slightly different as TypeScript attempts to stricly be a superset of JavaScript, which is more accurate to refer to as transpiling. – Travis J Feb 13 '20 at 22:46
  • @TravisJ that is fair enough, on further inspection it turns out "transpilers" are a subset of "compilers" and their definition is muddy anyway. I do still think it's most accurate to call Babel and TS "source-to-source compilers" (which is another name for "transpiler") since they both compile to readable javascript source that can be interpreted by any JS interpreter on the target platform. My issue was really just with the phrase "only compiled JavaScript was using this" - compiled JS is a thing, as the V8 engine compiles it down to bytecode, and that is not what Babel does at all – Klaycon Feb 13 '20 at 22:56
  • @Klaycon - My phrasing definitely could have been more accurate as well, because you are correct with regards to the fact that JavaScript gets compiled into a more computer readable form (Node, Browser, App, etc.). I was mainly trying to indicate that tools such as Babel compile or transpile *into* JavaScript. – Travis J Feb 13 '20 at 23:00

0 Answers0