How can I differentiate between a 404 error and other HTTP error codes (e.g., 500, 403) when using `fetch`?

Responsive Ad Header

Question

Grade: Education Subject: Support
How can I differentiate between a 404 error and other HTTP error codes (e.g., 500, 403) when using `fetch`?
Asked by:
107 Viewed 107 Answers

Answer (107)

Best Answer
(283)
Check the `response.status` property. A 404 indicates 'Not Found', a 500 indicates 'Internal Server Error', and a 403 indicates 'Forbidden'. You can use a `switch` statement or `if/else` conditions to handle each error code differently based on its meaning and appropriate response.