Q1. What is destructuring?
Destructuring is a JavaScript feature that allows extracting values from arrays or properties from objects and storing them into variables.
Destructuring makes code cleaner and shorter by avoiding repeated access to object properties or array elements.
Destructuring makes code cleaner and shorter by avoiding repeated access to object properties or array elements.
Q2. What is Array Destructuring?
Array destructuring allows extracting values from an array and assigning them to variables.
Example
Values from the array are assigned based on their position.
Example
const numbers = [10, 20, 30];
const [a, b, c] = numbers;
console.log(a);console.log(b);console.log(c);Values from the array are assigned based on their position.
Q3. What is Object Destructuring?
Object destructuring allows extracting properties from an object and assigning them to variables.
Example
Variables are matched using property names instead of positions.
Example
const person = { name: "John", age: 25};
const { name, age } = person;
console.log(name);console.log(age);Variables are matched using property names instead of positions.
Q4. Can you assign default values in destructuring?
Yes, default values can be assigned if the extracted value is undefined.
Example
Since age is not present, the default value is used.
Example
const user = { name: "Mike"};
const { name, age = 18 } = user;
console.log(age);Since age is not present, the default value is used.
Q5. Can you skip values in Array Destructuring?
Yes, values can be skipped using commas.
Example
Example
const numbers = [1, 2, 3, 4];
const [first, , third] = numbers;
console.log(first);console.log(third);