Skip to content
This repository has been archived by the owner on Jan 28, 2023. It is now read-only.

[docs] Include examples not related to ?. #2

Closed
rattrayalex opened this issue Jul 23, 2017 · 5 comments · Fixed by #25
Closed

[docs] Include examples not related to ?. #2

rattrayalex opened this issue Jul 23, 2017 · 5 comments · Fixed by #25

Comments

@rattrayalex
Copy link

This is a proposal that would be beneficial regardless of whether ?. passes.

eg;

const foo = getMaybeFoo() ?? defaultFoo
@ljharb
Copy link
Member

ljharb commented Jul 23, 2017

Agreed - this operator is useful on its own.

It's also particularly useful in jsx with React:

<div>
  {ifZeroThisIsAFootgun && bar}
  {!!thisIsTheCurrentWorkaround && bar}
  {orThis ? bar : null}
  {butThisIsAlwaysSafe ?? bar}
</div>

@Mouvedia
Copy link

I find foo ?? false super useful. I needed something like that last week.

@alangpierce
Copy link

@ljharb I think in your example you actually want a different operator. true ?? <div /> will be true, which isn't what you want.

?? is a more precise version of || for implementing the "default value" use case, and I think you want an operator that's a more precise version of && for implementing the "conditional value" use case. I guess it would be a OP b meaning a ? b : null (or undefined). That's somewhat related to #4 in that both attempt to be like &&, although the operator proposed there wouldn't fit that use case.

One way of thinking about it is that, for these use cases, || is imprecise in how it deals with inputs, whereas && is imprecise in the output it produces.

@ljharb
Copy link
Member

ljharb commented Jul 25, 2017

@alangpierce that is in fact exactly what I want, because in the common use case, the first operand is always null or a React element.

@ScottRudiger
Copy link

[docs] Include examples not related to ?.

This is a proposal that would be beneficial regardless of whether ?. passes.

eg;

const foo = getMaybeFoo() ?? defaultFoo

Seconding this as it wasn't clear to me from the README alone that this would be possible (had to look through the issues).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
5 participants