Reputation: 5736
Consider the following ES6 code:
function foo({name, address: {street, postcode}}) {
console.log(name, street, postcode);
}
foo({name: 'John', address: {street: 'Foo', postcode: 1234}});
foo({name: 'Bob'});
The first call works as expected. However, I would like to make address
optional (street
and postcode
would be undefined
) instead of throwing an error. Is this possible?
Upvotes: 5
Views: 568
Reputation: 5736
I found the solution:
function foo({name, address: {street, postcode} = {}}) {
console.log(name, street, postcode);
}
Upvotes: 6