I would advise against variable definitions during destructing like this:
const{name,dept,message=`${name} is ${dept}`}=employee;
What you're actually doing here is simply defining a default value for employee's property message. So as a side effect if your object actually has a property with that name, it will simply override your default value.
constemployee={name:'John',dept:'Service',message:'This is me!'}const{name,dept,message=`${name} is ${dept}`}=employee;console.log(message);// logs 'This is me!'
It works, and you could still use it, but be aware of what's actually happening and consider writing 1 line of extra code to prevent possible unexpected behaviour.
Apart from that – A good overview to destructing. Gj!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I would advise against variable definitions during destructing like this:
What you're actually doing here is simply defining a default value for employee's property
message
. So as a side effect if your object actually has a property with that name, it will simply override your default value.It works, and you could still use it, but be aware of what's actually happening and consider writing 1 line of extra code to prevent possible unexpected behaviour.
Apart from that – A good overview to destructing. Gj!