Optional Properties

Optional Properties
πŸ‘¨β€πŸ’Ό Excellent! You've handled optional data safely.
πŸ¦‰ Key techniques for optional properties:
  1. Optional chaining (?.) - Returns undefined if property is missing
  2. Nullish coalescing (??) - Provides a default value
  3. Type narrowing - Check with if before using
// These are all safe ways to handle optional properties:
user.bio?.toUpperCase() // undefined if no bio
user.bio ?? 'Default bio' // 'Default bio' if no bio
if (user.bio) {
	console.log(user.bio)
} // Only runs if bio exists
Optional properties are essential for modeling real-world data where not every field is always present.

Please set the playground first

Loading "Optional Properties"
Loading "Optional Properties"
Login to get access to the exclusive discord channel.
Loading Discord Posts