Following code gives empty object {}
:
{ 2, {...undefined}}
But why does below code gives error: Uncaught TypeError: undefined is not iterable
?
{ 2, [...undefined]}
Following code gives empty object {}
:
{ 2, {...undefined}}
But why does below code gives error: Uncaught TypeError: undefined is not iterable
?
{ 2, [...undefined]}
You cannot destructure an array if it's not an array for sure. It's ok to have an empty array so the given value will be undefined.
You can use this to prevent access undefined OR non-iterable value:
const a = undefined
console.log(...(a ?? []))