0

I have this code in javascript:

val: a ? a : ''

Basically checking that a is not null before assinging it.

The problem is that a is double in the line. Is there a shorter way to do this like:

val: a ? ''

? Thanks

Shali
  • 41
  • 1
  • 5
  • How about `a ?? ""`? The semantics are different though: `a ? a : ""` isn't checking if `a` is not null, it's checking if `a` is falsey which happens to include null. `a ?? ""` is also checking undefined in addition to null. You may want `a === null ? "" : a` for maximum precision. – ggorlen Oct 08 '20 at 04:58
  • 2
    with or `val: a || ''` – epascarello Oct 08 '20 at 05:02
  • You can const val = a || ' '; – MubashirEbad Oct 08 '20 at 05:07

0 Answers0