Understanding Responses
Learn to read HTTP status codes, check response success, and inspect headers — essential skills for robust data fetching.
HTTP Status Codes: What the Server Tells You
Every HTTP response comes with a status code — a three-digit number that tells you what happened. You've probably seen "404 Not Found" before (maybe on a broken link). That 404 is a status code. Understanding these codes is essential because your JavaScript needs to react differently depending on what the server says.
Status codes are grouped by their first digit:
- 2xx (Success) — Everything worked.
200 OKis the most common: "Here's the data you asked for." - 3xx (Redirect) — The resource moved. The browser usually handles these automatically.
- 4xx (Client Error) — You made a mistake.
404 Not Foundmeans the URL doesn't exist.401 Unauthorizedmeans you need to log in.403 Forbiddenmeans you don't have permission. - 5xx (Server Error) — The server broke.
500 Internal Server Errormeans something went wrong on the server's end, and there's nothing you can do except try again later.
Here's the important part: fetch() does NOT reject on HTTP errors like 404 or 500. It only rejects on network failures (the server is unreachable). A 404 response is still a successful HTTP exchange — the server responded, it just said "I don't have that." This is a common source of confusion for beginners.
HTTP status codes are defined in RFC 9110. The fetch() specification deliberately chose not to reject on HTTP errors to give developers full control over error handling. Always check response.ok yourself.