-2

I'm working on some javascript legacy code and I came across this conditional, can anyone tell me what does the '?' do? The 'items' property is not part of the currentItem object and it never is...

if (currentItem?.items)
  • 1
    The optional chaining means that there won’t be an error “can’t read items of undefined”if currentItem is undefined. – JBallin Jul 22 '20 at 18:33

1 Answers1

2

See: Optional Chaining in JavaScript


That's the optional chaining operator.

The optional chaining operator (?.) permits reading the value of a property located deep within a chain of connected objects without having to expressly validate that each reference in the chain is valid.

Read more: Optional chaining (?.) (MDN)

charles
  • 1,534
  • 1
  • 11
  • 19