The destructuring assignment in JavaScript is a syntax that allows unpacking values from arrays or properties from objects into distinct variables. It makes code more concise and easier to read when extracting data.
Array destructuring extracts values from an array and assigns them to variables.
Example:
const fruits = ["Apple", "Banana", "Cherry"]; const [first, second, third] = fruits; console.log(first); // Output: Apple console.log(second); // Output: Banana console.log(third); // Output: Cherry
You can skip elements by leaving empty spaces between commas.
Example:
const numbers = [1, 2, 3, 4, 5]; const [first, , third] = numbers; console.log(first); // Output: 1 console.log(third); // Output: 3
Default values can be used if an array element is undefined.
Example:
const colors = ["Red"]; const [primary, secondary = "Blue"] = colors; console.log(primary); // Output: Red console.log(secondary); // Output: Blue
Use the rest operator ... to collect remaining elements into an array.
Example:
const numbers = [1, 2, 3, 4]; const [first, ...rest] = numbers; console.log(first); // Output: 1 console.log(rest); // Output: [2, 3, 4]
Object destructuring extracts properties from an object into variables.
Example:
const person = { name: "Alice", age: 25, country: "USA" }; const { name, age } = person; console.log(name); // Output: Alice console.log(age); // Output: 25
You can rename variables while destructuring using a colon (:).
Example:
const person = { name: "Alice", age: 25 }; const { name: fullName, age: years } = person; console.log(fullName); // Output: Alice console.log(years); // Output: 25
Default values can be used if a property is undefined.
Example:
const person = { name: "Alice" }; const { name, age = 30 } = person; console.log(name); // Output: Alice console.log(age); // Output: 30
You can destructure properties from nested objects.
Example:
const employee = { id: 101, details: { name: "Bob", position: "Developer" }, }; const { details: { name, position }, } = employee; console.log(name); // Output: Bob console.log(position); // Output: Developer
Use the rest operator to collect remaining properties.
Example:
const person = { name: "Alice", age: 25, country: "USA" }; const { name, ...others } = person; console.log(name); // Output: Alice console.log(others); // Output: { age: 25, country: "USA" }
You can combine array and object destructuring.
Example:
const data = { id: 1, items: ["Apple", "Banana", "Cherry"], }; const { id, items: [firstItem], } = data; console.log(id); // Output: 1 console.log(firstItem); // Output: Apple
You can destructure arrays or objects directly in function parameters.
A. Destructuring Arrays in Parameters:
function sum([a, b]) { return a + b; } console.log(sum([5, 10])); // Output: 15
B. Destructuring Objects in Parameters:
const fruits = ["Apple", "Banana", "Cherry"]; const [first, second, third] = fruits; console.log(first); // Output: Apple console.log(second); // Output: Banana console.log(third); // Output: Cherry
const numbers = [1, 2, 3, 4, 5]; const [first, , third] = numbers; console.log(first); // Output: 1 console.log(third); // Output: 3
const colors = ["Red"]; const [primary, secondary = "Blue"] = colors; console.log(primary); // Output: Red console.log(secondary); // Output: Blue
const numbers = [1, 2, 3, 4]; const [first, ...rest] = numbers; console.log(first); // Output: 1 console.log(rest); // Output: [2, 3, 4]
Mastering destructuring assignment makes your JavaScript code more readable and efficient.
Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.
The above is the detailed content of Demystifying Destructuring Assignment in JavaScript. For more information, please follow other related articles on the PHP Chinese website!