🟨 JavaScript
Beginner
What is destructuring in JavaScript?
Answer
Destructuring (ES6) unpacks values from arrays or properties from objects into distinct variables. Array destructuring: const [first, second, ...rest] = [1, 2, 3, 4] — first=1, second=2, rest=[3,4]. Skip elements: const [,, third] = arr. Default values: const [a = 10] = []. Object destructuring: const { name, age, city = "Unknown" } = user. Rename: const { name: userName } = user. Nested: const { address: { street } } = user. In function parameters: function greet({ name, age }) { }. Destructuring makes code cleaner, reduces repetition, and is widely used with React hooks, API responses, and module imports. Swap variables: [a, b] = [b, a].