destructure parameter javascript code example
Example 1: javascript function destructuring
function f() {
return [1, 2];
}
let a, b;
[a, b] = f();
console.log(a);
console.log(b);
Example 2: how destructuring works in javascript
const objA = {
prop1: 'foo',
prop2: {
prop2a: 'bar',
prop2b: 'baz',
},
};
const { prop1, prop2: { prop2a, prop2b } } = objA;
console.log(prop1);
console.log(prop2a);
console.log(prop2b);
Example 3: js object destructuring with defaults
const { dogName = 'snickers' } = { dogName: undefined }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: null }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: false }
console.log(dogName)
const { dogName = 'snickers' } = { dogName: 0 }
console.log(dogName)