DEV Community 👩‍💻👨‍💻

Discussion on: Destructuring Assignment in ES6- Arrays

Collapse
 
kodekage profile image
Prosper Opara

This is an awesome piece..

I have some questions

let [greet = "hello world", name = "sarah"] = ["hello"];

console.log(greet); // returns "hello"

why is is overriding the default value assigned to hello?

Collapse
 
sayopaul profile image
Sayo Paul

I am not sure I understand your question fully but like the article explains , in this case , the array contains one element , "hello" and thus it overrides the default value of the first variable greet whilst the second variable is its default value as the array doesn't have a second element 🙂 .

Collapse
 
shrijan00003 profile image
Shrijan

greet = "hello world" is just a default value , if there is the value in first element in parent array then it will get overridden for sure. That is what default value is made for.

Collapse
 
malvinjay profile image
George Arthur

I think the question isn't that clear. Variables available are 'greet' and 'name' to which they both have default values. However, "greet" is assigned a value from Destructuring and thus will now return that value i.e ("hello") instead of it's default value give at initialization.