Category: java
Destructuring is a JavaScript
Published on 04 Mar 2026
Explanation
Destructuring is a JavaScript feature that
extract values from objects or arrays
Example:
'name' and 'role' are extracted from
the user object.
Code:
const user = { id: 1,
name: 'hackforge',
role: 'edtech'
};
const { name, role } = user;
//name is hackforge
Explanation
Array destructuring allows you to
extract values from
arrays based on position.
Here, 'first' gets 10
and 'second' gets 20.
Code:
const numbers = [10, 20, 30]; const [first, second] = numbers;
Explanation
In Node.js (Express), destructuring is
commonly used to
extract values
from req.body, req.params, or req.query.
makes the code cleaner and avoids
repeatedly writing
req.body.email, req.body.password, etc.
Code:
app.post('/login', (req, res) => {
const { email, password } = req.body;
});
Explanation
Destructuring in Node.js is also used to
extract environment variables
from process.env.
Default values can be
assigned if the variable is undefined.
Code:
const { PORT = 3000 } = process.env;