Beginner
Parse JSON without crashing
Saved data can be broken. Write safeParse(text, fallback):
1. Return JSON.parse(text) when the text is valid JSON.
2. When it isn't, log a warning that starts with Bad JSON (use console.warn) and return fallback.
Careful: valid JSON such as "0" or "false" must return 0 or false, not the fallback.
What Check tests
- Valid JSON is parsed
- Broken JSON doesn't throw
- Broken JSON returns the fallback
- A warning starting with
Bad JSONis logged - Valid
0andfalseare kept
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Your page
Press Run to see the page.
Console
Nothing logged yet.