fetch(url) requests HTTP resources. response.json() parses JSON bodies—common for APIs you'll build on Node.js.
Basic flow
const res = await fetch('/api/items');
if (!res.ok) throw new Error(res.status);
const data = await res.json();
In this playground, practice parsing with JSON.parse; real fetch runs in the browser tab.
CORS
Browsers block cross-origin responses unless server sends CORS headers—backend topic.
Important interview questions and answers
- Q: res.ok?
A: True for status 200–299—still check before parsing. - Q: JSON.parse vs json()?
A: parse on string; json() on Response stream in fetch.
Self-check
- Why check res.ok?
- What is CORS in one sentence?
Challenge
Parse API-shaped JSON
- Parse the sample JSON string.
- Log items.length.
- Add a guard if items is missing.
Done when: your code handles missing items without throwing.
Interview prep
- res.ok?
True for HTTP 2xx—check before parsing body.