0% found this document useful (0 votes)
6 views1 page

JavaScript Destructuring Explained

Destructuring in JavaScript allows for the extraction of values from arrays and properties from objects into distinct variables, promoting cleaner and more readable code. It includes techniques such as skipping values, setting default values, and renaming variables during destructuring. Examples illustrate both array and object destructuring with various use cases.

Uploaded by

sanjaystar14581
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views1 page

JavaScript Destructuring Explained

Destructuring in JavaScript allows for the extraction of values from arrays and properties from objects into distinct variables, promoting cleaner and more readable code. It includes techniques such as skipping values, setting default values, and renaming variables during destructuring. Examples illustrate both array and object destructuring with various use cases.

Uploaded by

sanjaystar14581
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Destructuring in JavaScript

Definition:

Destructuring is a convenient way of extracting values from arrays or properties from objects into distinct variables. It
helps write cleaner, shorter, and more readable code.

Array Destructuring

Example:
const arr = [1, 2, 3];
const [a, b, c] = arr;
[Link](a); // 1
[Link](b); // 2
[Link](c); // 3

Skipping values:
const [x, , z] = [10, 20, 30];
[Link](x); // 10
[Link](z); // 30

Default values:
const [p = 5, q = 10] = [undefined];
[Link](p); // 5
[Link](q); // 10

Object Destructuring

Example:
const user = { name: "Nilam", age: 24 };
const { name, age } = user;
[Link](name); // "Nilam"
[Link](age); // 24

Rename while destructuring:


const { name: username } = user;
[Link](username); // "Nilam"

Default values:
const { email = "Not Provided" } = user;
[Link](email); // "Not Provided"

You might also like