Destructuring Assignment in JavaScript (ES6) β Extract Object Values Easily
π INTRODUCTION
Destructuring assignment is a powerful feature introduced in ES6 that allows you to extract values from objects and arrays in a clean and concise way.
Instead of accessing properties one by one, destructuring lets you:
- Write shorter and cleaner code
- Improve readability
- Work efficiently in React and modern JavaScript
π PROBLEM STATEMENT
π Write a program to extract values from an object using destructuring assignment.
π‘ CONCEPTS USED
- Object Destructuring
- ES6 Syntax
- Variables and Objects
π§ LOGIC EXPLANATION
- Create an object with multiple properties
- Use destructuring syntax
{} - Extract values directly into variables
- Display the result
π» PROGRAM CODE (Using Destructuring)
const student = {
name: "Kanchan",
age: 18,
course: "BCA"
};
// Destructuring assignment
const { name, age, course } = student;
console.log("Name:", name);
console.log("Age:", age);
console.log("Course:", course);
π₯οΈ OUTPUT
Name: Kanchan Age: 18 Course: BCA
π STEP-BY-STEP EXPLANATION
{ name, age, course }extracts values from object- Variable names must match object keys
- Values are directly assigned to variables
β‘ OLD METHOD (WITHOUT DESTRUCTURING)
const student = {
name: "Kanchan",
age: 18,
course: "BCA"
};
const name = student.name;
const age = student.age;
const course = student.course;
console.log(name, age, course);
π― ADVANCED EXAMPLES
1. Rename Variables
const { name: studentName, age: studentAge } = student;
console.log(studentName, studentAge);
2. Default Values
const { city = "Delhi" } = student;
console.log(city);
3. Array Destructuring
const numbers = [10, 20, 30];
const [a, b, c] = numbers;
console.log(a, b, c);
π ADVANTAGES OF DESTRUCTURING
- Reduces code length
- Improves readability
- Avoids repetitive object access
- Widely used in React props and APIs
π REAL-WORLD USE CASE
- Extracting API response data
- Handling React props
- Working with JSON objects
β VIVA QUESTIONS (IMPORTANT)
Q1. What is destructuring in JavaScript?
π A way to extract values from objects or arrays into variables.
Q2. Which ES version introduced destructuring?
π ES6 (ECMAScript 2015)
Q3. Can we rename variables during destructuring?
π Yes, using key: newName.
Q4. Can destructuring be used with arrays?
π Yes, using square brackets [].
Q5. What happens if a property does not exist?
π It returns undefined unless a default value is provided.
π RELATED POSTS
π MCA 168 Full Stack Development Lab Programs List
π Arrow Function in JavaScript β Program 1
π Template Literals in JavaScript β Program 2
π CONCLUSION
Destructuring assignment is an essential concept in modern JavaScript. It simplifies code and is heavily used in frameworks like React.
π Master this concept to write cleaner and more efficient programs.
Further Reading
How to Master Full Stack Development?
Spring Framework Practice Problems and Their Solutions
From Google to the World: The Story of Go Programming Language
Why Go? Understanding the Advantages of this Emerging Language
Creating and Executing Simple Programs in Go
20+ Interview Questions on Go Programming Language
100+ MCQs On Java Architecture
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
