Understanding Responses

Learn to read HTTP status codes, check response success, and inspect headers — essential skills for robust data fetching.

Step 1 of 5

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 OK is 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 Found means the URL doesn't exist. 401 Unauthorized means you need to log in. 403 Forbidden means you don't have permission.
  • 5xx (Server Error) — The server broke. 500 Internal Server Error means 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.

Think of it this way: Status codes are like delivery receipts. A 200 is a signed receipt: 'Package delivered successfully.' A 404 is 'Address not found — returning to sender.' A 500 is 'Delivery truck broke down.' Just because the postal service delivers a 'not found' notice doesn't mean the postal service failed — it completed its job of telling you the outcome.
Web Standard
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.