×

typescript · level 1 · unit 2 · lesson 4

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

lets your program ask a question.

prompt is like a waiter taking your order. It asks a question, waits while the person types an answer, then hands back what they typed as a string.

But what if nobody answers? Then prompt hands back , which means no value at all. So its type is string | null: a string, or null.

A string box cannot hold null. Add and a fallback, something to use instead. prompt("Snack?") ?? "nothing" gives the answer, or "nothing" when there is none.

let snack: string = prompt("Favourite snack?") ?? "nothing";
console.log("Yum, " + snack);

Read ?? as “or, if there is no answer, use”.