Destructuring and spread, in depth
You should see
200 Ada admin [ 'dev' ] 1 20
1 Ada x
2 Bob none
[ 'Ada', 'Bob' ]
2 1 20 [ 30, 40 ]
{ id: 7, name: 'Grace', roles: [ 'admin', 'dev', 'ops' ] } Ada
{ a: 1, b: 2, c: 3 }
[ 3, 1 ] [ 'h', 'e', 'y' ] 9
7 { name: 'Ada', roles: [ 'admin', 'dev' ] }
2 9pick(obj, ...keys) and omit(obj, ...keys) using Object.entries, filter and Object.fromEntries.TypeError: Cannot destructure property 'name' of 'undefined'
function greet({ name }) { return `hi ${name}` }
greet()Uncaught TypeError: Cannot destructure property 'name' of 'undefined' as it is undefined.
at greet (your code:1)Destructuring reads properties, and you cannot read a property of undefined. The caller passed nothing.
Give the whole parameter a default: function greet({ name = "friend" } = {}). The = {} handles the missing argument; name = … handles the missing field.
function greet({ name = "friend" } = {}) { return `hi ${name}` }
console.log(greet(), greet({ name: "Ada" }))