destructuring assignment default value [duplicate]

You probably confused by the difference between null and undefined, For example:

const { dogName="snickers" } = { dogName: undefined }
console.log(dogName) // what will it be? 'snickers'!

const { dogName="snickers" } = { dogName: null }
console.log(dogName) // what will it be? null!

const { dogName="snickers" } = { dogName: false }
console.log(dogName) // what will it be? false!

const { dogName="snickers" } = { dogName: 0 }
console.log(dogName) // what will it be? 0!

const { dogName="snickers" } = { }
console.log(dogName) // what will it be? 'snickers'!

Taken from: http://wesbos.com/destructuring-default-values/

Leave a Comment