Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #38413

Re: check for obj nested prop

Newsgroups comp.lang.javascript
Date 2020-05-13 12:30 -0700
References <C9udnTlJHfCQbSnDnZ2dnUU7-U3NnZ2d@westnet.com.au> <70e81d39-3e56-4366-8d8a-b7366dcfb715@googlegroups.com> <H_CdneUVbcFZ-SbDnZ2dnUU7-SHNnZ2d@westnet.com.au>
Message-ID <bdde63fd-8374-467b-ac76-0508a16a10bc@googlegroups.com> (permalink)
Subject Re: check for obj nested prop
From "Michael Haufe (TNO)" <tno@thenewobjective.com>

Show all headers | View raw


On Tuesday, May 12, 2020 at 10:09:03 PM UTC-5, Andrew Poulos wrote:
>
> Just what I needed. The page mentions the "nullish coalescing operator" 
> for which it gives the example
>    const customerCity = customer?.city ?? "Unknown city";
> 
> I've not used that operator before. What I would've done is
>    const customerCity = customer?.city || "Unknown city";
> 
> Is using the former operator "better" than the later?

The (||) operator works on falsey values and the (??) operator works on null|undefined. In most cases you'll want to get in the habit of using (??) instead as you'll avoid more bugs that way. An example of a bug:

```js
let height = rectangle.height || 10
```

If `rectangle.height` is '0' then `height` would be set to `10` which is probably not your intention when you just wanted a default. Using (??) would avoid this problem:

```js
let height = rectangle.height ?? 10
```

So it's 'better' to use (??).

Philosophically I think both cases are tactically smart but strategically dumb; `null` and `undefined` should be prevented from existing in the first place:

<https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/>

You do so by making your models (you domain) doesn't have them in the first place.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

check for obj nested prop Andrew Poulos <ap_prog@hotmail.com> - 2020-05-08 15:37 +1000
  Re: check for obj nested prop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-05-08 10:52 +0100
    Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-09 00:26 +0200
      Re: check for obj nested prop Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-05-09 03:09 +0100
        Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-12 09:28 +0200
          Re: check for obj nested prop Julio Di Egidio <julio@diegidio.name> - 2020-05-12 04:01 -0700
    Exception handling (was: check for obj nested prop) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-09 03:46 +0200
  Re: check for obj nested prop JJ <jj4public@vfemail.net> - 2020-05-08 17:18 +0700
  Re: check for obj nested prop Julio Di Egidio <julio@diegidio.name> - 2020-05-08 03:53 -0700
  Re: check for obj nested prop James Kirk <noneya.invalid@gmail.com> - 2020-05-08 11:00 -0400
  Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-09 00:19 +0200
    Re: check for obj nested prop Julio Di Egidio <julio@diegidio.name> - 2020-05-12 04:04 -0700
      Re: check for obj nested prop luserdroog <luser.droog@gmail.com> - 2020-05-12 07:59 -0700
  Re: check for obj nested prop "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-05-12 14:17 -0700
    Re: check for obj nested prop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2020-05-13 02:46 +0200
    Re: check for obj nested prop Andrew Poulos <ap_prog@hotmail.com> - 2020-05-13 13:08 +1000
      Re: check for obj nested prop "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2020-05-13 12:30 -0700

csiph-web