Skip to content

Instantly share code, notes, and snippets.

@srdjanRakic
Created August 29, 2019 07:21
Show Gist options
  • Save srdjanRakic/a9547d40fa44e1e1a60d10e9916bef6d to your computer and use it in GitHub Desktop.
Save srdjanRakic/a9547d40fa44e1e1a60d10e9916bef6d to your computer and use it in GitHub Desktop.
This feature enables readable and concise expression of property accesses with built-in nullish checking.
const object = { id: 123, names: { first: 'Srdjan', last: 'Rakic' }};
// With lodash's `_.get`:
const firstName = _.get(object, 'names.first'); // -> 'Srdjan'
const middleName = _.get(object, 'names.middle' : '(no middle name)'); // -> '(no middle name)'
// with optional chaining and nullish coalescing:
const firstName = object?.names?.first ?? '(no first name)'; // -> 'Srdjan'
const middleName = object?.names?.middle ?? '(no middle name)'; // -> '(no middle name)'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment