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.
Loading...
Related Quiz
- When using Jest to test React components, the ______ method is commonly used to render components in a test environment.
- Which of the following array methods does not mutate the original array in JavaScript?
- How can you create an object in JavaScript that does not inherit the prototype from Object?
- The aud claim in a JWT token represents the ________ for which the JWT is intended.
- When a JavaScript function is executed, a new execution context is created, and a special object called ________ is created.