0 day streak
javascript / intermediate
Snippet

Object Destructuring with Aliases and Defaults

Intermediate destructuring allows you to rename properties (aliases) while extracting them and provide fallback values if a property is missing in the source object.

snippet.js
javascript
1
2
3
4
5
6
7
8
9
10
11
12
const config = {
api_url: 'https://api.example.com',
retries: 5
};
const {
api_url: url,
retries,
timeout = 3000
} = config;
console.log(`URL: ${url}, Retries: ${retries}, Timeout: ${timeout}`);
nodejs
Breakdown
1
api_url: url,
Extracts the 'api_url' property but stores it in a new variable named 'url'.
2
timeout = 3000
Assigns the value 3000 to 'timeout' if it is undefined in the config object.