I recently learned about optional chaining in Javascript and have been making use of it in a React/NodeJS project. Works great.
I noticed I have been using it with arrays map
, even without thinking about it much -- it seemed a natural use (here items
is an array, or possibly undefined
)
{items?.map(postListItem => ....
That is, it will map if items
exists, but not if items
is undefined
, but would avoid any run-time errors if I were to call map
on undefined
Nonetheless I don't know if this is an acceptable use or whether I'm mis-using optional chaining. I searched for an answer but as of yet haven't been able to find one, which makes me suspect I'm mis-using it. Any info much appreciated!