javascript / beginner
Snippet
Object Destructuring
Destructuring is a convenient way to extract multiple properties from an object and assign them to variables in a single line. This is widely used in Node.js when requiring modules or handling configuration.
snippet.js
1
2
3
4
const user = { id: 1, username: 'dev_markus', role: 'admin' };const { username, role } = user;console.log(username);
nodejs
Breakdown
1
const { username, role } = user;
Extracts 'username' and 'role' properties from the 'user' object into standalone variables.
2
console.log(username);
Prints the value of the newly created 'username' variable.