Object Destructuring Defaults in JavaScript
Level 4238 keystrokes
Object destructuring with defaults provides fallback values when properties are undefined or missing.
JavaScript code
// Destructured parameter with defaults; = {} allows calling with no argument1function createUser({ name = "Guest", age = 18, role = "user" } = {}) { // Return object using property shorthand2 return { name, age, role };3}// Call with partial object - uses defaults for missing4console.log(createUser({ name: "Alice" }));// Call with no argument - uses all defaults5console.log(createUser());// Call with all properties - no defaults used6console.log(createUser({ name: "Bob", age: 25, role: "admin" }));Output
{ name: 'Alice', age: 18, role: 'user' }
{ name: 'Guest', age: 18, role: 'user' }
{ name: 'Bob', age: 25, role: 'admin' }Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this JavaScript code and how often they appear.
"× 10(× 7)× 7,× 6{× 6}× 6;× 4:× 4=× 4.× 3