DEV Community

Cover image for JavaScript : Destructuring (English/Hindi)
Dharmik Dholu
Dharmik Dholu

Posted on

JavaScript : Destructuring (English/Hindi)

English

Destructuring in JavaScript is a feature that allows you to extract values from objects or arrays and assign them to variables in a concise and readable way. This can make your code cleaner and more organized. Let's look at an example:

Suppose you have an object representing a person:

const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
};
Enter fullscreen mode Exit fullscreen mode

With destructuring, you can easily extract the values from this object and assign them to variables:

const { firstName, lastName, age } = person;

console.log(firstName);  // Output: John
console.log(lastName);   // Output: Doe
console.log(age);        // Output: 30
Enter fullscreen mode Exit fullscreen mode

This is equivalent to manually assigning the values like this:

const firstName = person.firstName;
const lastName = person.lastName;
const age = person.age;
Enter fullscreen mode Exit fullscreen mode

Destructuring works similarly for arrays as well. Consider this example with an array:

const numbers = [1, 2, 3, 4, 5];

const [first, second, ...rest] = numbers;

console.log(first);  // Output: 1
console.log(second); // Output: 2
console.log(rest);   // Output: [3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Here, the first and second elements of the array are extracted and assigned to variables, and the rest of the elements are collected into the rest array.

Hindi

JavaScript mein destructuring ek feature hai jo aapko objects ya arrays se values ko extract karke unhe variables mein assign karne ki suvidha deta hai, aur yeh ek sankshep aur padhne mein asaan tareeke se hota hai. Isse aapka code saaf aur vyavasthit ho sakta hai. Chaliye ek udaharan dekhein:

Mana ki aapke paas ek vyakti ko darshane wala object hai:

const person = {
  firstName: 'John',
  lastName: 'Doe',
  age: 30,
};
Enter fullscreen mode Exit fullscreen mode

Destructuring ke dwara, aap aasani se is object se values ko nikal kar unhe variables mein assign kar sakte hain:

const { firstName, lastName, age } = person;

console.log(firstName);  // Output: John
console.log(lastName);   // Output: Doe
console.log(age);        // Output: 30
Enter fullscreen mode Exit fullscreen mode

Yeh manuaaly values ko aise assign karne ke barabar hai:

const firstName = person.firstName;
const lastName = person.lastName;
const age = person.age;
Enter fullscreen mode Exit fullscreen mode

Destructuring arrays ke liye bhi similarly kaam karta hai. Yeh ek array ke saath wala udaharan dekhein:

const numbers = [1, 2, 3, 4, 5];

const [first, second, ...rest] = numbers;

console.log(first);  // Output: 1
console.log(second); // Output: 2
console.log(rest);   // Output: [3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Yahan par, array ke pehle aur doosre elements ko extract karke variables mein assign kiya gaya hai, aur baki ke elements ko rest array mein collect kiya gaya hai.

Top comments (0)