Js/Es6: Destructuring of Undefined

JS/ES6: Destructuring of undefined

You can use short circuit evaluation to supply a default if content is a falsy value, usually undefined or null in this case.

const content = undefined
const { item } = content || {}
console.log(item) // undefined

How to handle destructuring of function argument with undefined in javascript?

A default value can be used

const product = {    label: 'Notebook',    price: 50};
const destructSample = ({label, price}= {}) => { console.log(label, price);}
destructSample(product);destructSample(undefined);

How to destructure nested object that might be undefined in javascript

You could take a default object.

const
myFunction = () => ({}),
{ data: { key1 } = {} } = myFunction();

console.log(key1);

Why am I getting 'undefined is not a function' for this destructuring assignment code?

The value you are destructuring is an object, not an array. Therefore you need to use the object destructuring syntax. If you use the wrong flavor you get the not very helpful "undefined is not a function" error.

const {p = 1, t = 25, ...otherParams} = request.query

I struggled with this very same error last Friday.



Related Topics



Leave a reply



Submit