Optional Chaining in JavaScript
Level 3126 keystrokes
Optional chaining (?.) safely accesses nested properties without errors if undefined.
JavaScript code
// Create a user object with nested address1const user = { name: "Alice", address: { city: "Tokyo" } };// Optional chaining ?. checks if address exists before accessing city// Returns "Tokyo" because address exists2console.log(user.address?.city);// contact doesn't exist, so returns undefined instead of throwing error// Without ?. this would throw: Cannot read properties of undefined (reading 'phone')3console.log(user.contact?.phone);Output
Tokyo undefined
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.
.× 6"× 4;× 3:× 3?× 2(× 2)× 2{× 2}× 2,× 1