WeCodeAcademy

void, null and undefined

A function that returns nothing has the return type void. With strict checking (on here), null and undefined are separate types, so you must handle "no value" explicitly.

Exampletypescript
Open in editor »

This is the biggest bug-saver in TypeScript: it will not let you forget the "no value" case.

Exampletypescript
Open in editor »
Quick check

What does `string | undefined` tell you?

Your turn: write the code

findUser(id) returns "Ada" for id 1 and undefined otherwise. Give it the right return type, then print findUser(2) ?? "not found".

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.