Let’s be honest for a second.
If your JavaScript still looks like this:
const user = { name: "Alex", age: 25 };
const name = user.name;
const age = user.age;
…we need to talk. 😅
Because JavaScript already gave us a much cleaner feature called Object Destructuring, and ignoring it is basically choosing more code for no reason.
Today we’ll learn:
What object destructuring is
How it works
What exactly happens behind the scenes
By the end, your code will be shorter, cleaner, and way more professional.
What is Object Destructuring?
In simple words:
Object destructuring lets you extract object properties into variables in one line.
Basic Example
const user = {
name: "Alex",
age: 25,
country: "India"
};
const { name, age, country } = user;
console.log(name, age, country);
Output
Alex 25 India
Explanation
The line:
const { name, age, country } = user;
means:
Take the name, age, and country properti
Discussion
Jump in and comment!
Get the ball rolling with your comment!