1

I have recently seen ?? in JS, or maybe it's a Typescript thing. It seems to behave the same as ||

code looks something like this

import GeneralImage from './images'

const Team = ({ teamImg }) => {
   return <img src={teamImg ?? GeneralImage} />
}

I would have thought to have done the following, but maybe there is a difference. Cannot find anything online for ?? but the linter doesn't seem bothered and it works the same.

const Team = ({ teamImg }) => {
   return <img src={teamImg || GeneralImage} />
}
peter flanagan
  • 6,130
  • 13
  • 50
  • 94
  • [nullish coalescing operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator). You didn't have to delete your question... – Patrick Roberts Feb 12 '20 at 16:56
  • @PatrickRoberts ah ok. When I try the demo on the man page I see the following `Error: Unexpected token '?'` – peter flanagan Feb 12 '20 at 16:58
  • becuase: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator#Browser_compatibility – epascarello Feb 12 '20 at 16:59
  • 3
    Don't use `??`. It has very low support - effectively a FF extension at this point as part of a proposal. – user2864740 Feb 12 '20 at 17:01
  • just saw it's not supported in safari - thanks – peter flanagan Feb 12 '20 at 17:01
  • is it a new feature? Just supported for Firefox? @user2864740 – peter flanagan Feb 12 '20 at 17:01
  • 2
    @peterflanagan It's not defined in any published ECMAScript standard, including ES6. Right now, *`??` is a vendor-specific non-standard language extension*. So yes, it's a "feature" of FF that should not be generally used. – user2864740 Feb 12 '20 at 17:02
  • and @PatrickRoberts is it just to cover cases if you consider 0, '', or NaN as valid values? – peter flanagan Feb 12 '20 at 17:04
  • @user2864740 — Given this is the year of ES11, it isn't much of a surprise that ES6 doesn't have bleeding edge features :) – Quentin Feb 12 '20 at 17:04
  • @user2864740 "including ES6"... that seems irrelevant. The only relevant standard in my opinion would be ES2020 – Patrick Roberts Feb 12 '20 at 17:04
  • @PatrickRoberts Then tag the question appropriately and add disclaimers. Now, back in the real world, I've some coffee to refill.. – user2864740 Feb 12 '20 at 17:05
  • @user2864740 I'm not OP, didn't tag it... – Patrick Roberts Feb 12 '20 at 17:05
  • updated the tag. Tagged it with ES6 as was unsure if it was a new feature that I had missed – peter flanagan Feb 12 '20 at 17:06
  • 1
    @peterflanagan — It is new. It's just a Stage 4 Proposal which will probably be in ES2020, not part of the ES6 spec from 2015. – Quentin Feb 12 '20 at 17:09
  • thanks @Quentin The tag has been removed so everyone can rest easy tonight – peter flanagan Feb 12 '20 at 17:09
  • @PatrickRoberts once introduced can you see it been used more often than the more traditional `||` – peter flanagan Feb 12 '20 at 17:12
  • @peterflanagan We have null coalesce in PHP and I use it as a switch. Especially if an argument is typed, for instance (`?string $name`), you can do a switch-thru (like fall-through, like Maybe or Optionals) to respond to a non-property: `$fullname = $name ?? $user['name'];` It is not the same, at least in PHP, as a falsey optional, which is `?:` and called a short ternary. – Jared Farrish Feb 12 '20 at 17:16
  • @Quentin not "probably", [it already is](https://tc39.es/ecma262/#prod-CoalesceExpression) – Patrick Roberts Feb 12 '20 at 17:51
  • @PatrickRoberts do you predict it to be used over `||` once released? – peter flanagan Feb 12 '20 at 18:20
  • @peterflanagan asking for opinions is [off-topic](https://stackoverflow.com/help/on-topic). – Patrick Roberts Feb 12 '20 at 19:24
  • @PatrickRoberts whoops, sorry for breaking the rules where is the best place I can ask you for your opinion? Do you have an email address, or maybe a telephone number I could contact you on? – peter flanagan Feb 12 '20 at 22:21

0 Answers0