While working on a project, you are required to extract specific properties from objects and assign them to variables. How would you utilize destructuring assignment to efficiently accomplish this task, and what would you do to handle non-existent properties?

  • const { prop1, prop2 } = sourceObject;
  • const [prop1, prop2] = sourceObject;
  • const { prop1, prop2 } = [sourceObject];
  • const [prop1, prop2] = { ...sourceObject };
To efficiently extract specific properties from objects using destructuring assignment in JavaScript, you would use the syntax const { prop1, prop2 } = sourceObject;. This assigns the values of prop1 and prop2 from sourceObject to the corresponding variables. To handle non-existent properties, you can provide default values like const { prop1 = defaultValue1, prop2 = defaultValue2 } = sourceObject;. The other options are not correct ways to achieve this task.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *