Response.prototype.ok
Returns a boolean indicating whether the response was successful (status in the range 200-299)
Syntax
response.okReturn Value
A boolean: true for status 200-299, false otherwise
Examples
const response = await fetch('/api/data')
if (!response.ok) {
throw new Error(`Request failed: ${response.status}`)
}
const data = await response.json()async function safeFetch(url: string) {
const res = await fetch(url)
if (!res.ok) {
const body = await res.text()
throw new Error(`${res.status}: ${body}`)
}
return res.json()
}const res = await fetch('/api/check')
console.log('OK:', res.ok) // true if 2xx
console.log('Status:', res.status)
console.log('StatusText:', res.statusText)Understanding Response.prototype.ok
The Response.prototype.ok method in JavaScript returns a boolean indicating whether the response was successful (status in the range 200-299). It belongs to the Response object and is one of the most widely used methods for working with response values in modern JavaScript and TypeScript applications.
The method signature is response.ok. When called, it returns a boolean: true for status 200-299, false otherwise. Understanding when and how to use ok() helps you write more expressive, readable code.
Common use cases for Response.prototype.ok include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like fetch-response-json, fetch-response-status, request-constructor, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Response.prototype.ok is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
More Response Methods
Other methods in the Response object
Related Tools
More Response Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.