5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Optional Chaining lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.
Use optional chaining to read a city that is not there, and show its type.
Use optional chaining to read a city that is present.
Use optional chaining twice on a path that stops halfway.
Safely read the first item of an array that does not exist.
Show that optional chaining does not replace a value of zero.
The address is missing, so the path needs guarding.
The guard is on the wrong step of the path.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspaceAn index needs ?.[ ] rather than a plain dot.
The nested checks work but are hard to read; use optional chaining.
Optional chaining only guards null and undefined, so a real zero must survive.