Color theme
0 day streak0 XPP1
TypeScriptLearn mode
MISSION 1 OF 1

Display an optional photo caption

0 XP earned
TypeScript track: IntermediateMission: IntermediateOptional propertiesOptional chainingNull coalescing
Before you start this intermediate track

JavaScript objects and functions. The lessons introduce narrowing, unions, and optional properties.

Guided code reading and debugging. Runnable coding challenges are available in JavaScript, Python, and SQL.

x
0 / 2 challenges
LEARN THE CONCEPT

Understand it, then try it.

The question mark on a property means an object may omit it. Optional chaining calls toUpperCase only when caption exists; otherwise the expression produces undefined. Null coalescing then supplies a fallback for that missing result. Together these operators safely transform supplied text while handling a photo that has no caption.

mission.ts
type Photo = { caption?: string };const photo: Photo = {};const caption = photo.caption?.toUpperCase() ?? "UNTITLED";console.log(caption);

Use TypeScript 5+ with strict checking and an ES2020 target. Each mission is an independent file. Compile the TypeScript to JavaScript, then run it with Node.js or in a browser console.

Track the values line by line. Predict the result before running this example in your own editor.