5
useVim
3y

TIL about optional chaining in JS.

I can sleep better tonight.

Comments
  • 2
    here's what it solves.

    before:

    let myEl = document.querySelector('.my-elem') ? document.querySelector('.my-elem').innerHTML : null;

    after:

    let myEl = document.querySelector('.my-elem')?.innerHTML

    my life is changed forever.
  • 4
    Yup, it's awesome. Small correction - if not finding the chained thing, it will return undefined, not null.
  • 0
    the correct search term for this is "null-safe property access"
  • 0
    @webketje I can relate it with dart null safety
  • 1
    it?.is?.veryNice=true
Add Comment