How does destructuring work in JavaScript?
Suggested answer
Destructuring extracts values from arrays or objects into variables. Array destructuring: const [a, b, c = 10] = [1, 2]; — c defaults to 10. Skip elements with commas: const [, second] = [1, 2];. Object destructuring: const { name, age = 25 } = person; — defaults for missing properties. Renaming: const { name: fullName } = person; — extracts name as fullName. Nested: const { address: { city } } = person;. Rest in destructuring: const { a, ...rest } = obj; — rest contains all remaining properties. In function parameters: function greet({ name, age = 18 }) {...} — destructure directly in the parameter list. Destructuring is extensively used in LWC to handle wire service results and event detail objects.
Practice content for interview preparation; not an official vendor answer. Verify details against current product documentation.
Community comments (0)
No comments yet.
Sign in or create a free account to add a comment. Comments are moderated before they appear.