Response
Creates a new Response object representing a response to a request
Syntax
new Response(body?, init?)Parameters
| Parameter | Type | Description |
|---|---|---|
| body | BodyInit | null | The body of the response (Blob, ArrayBuffer, string, ReadableStream, etc.) |
| init | ResponseInit | Options: status, statusText, headers |
Return Value
A new Response object
Examples
const response = new Response(JSON.stringify({ ok: true }), {
status: 200,
headers: { 'Content-Type': 'application/json' }
})
console.log(response.status) // 200const notFound = new Response('Not Found', { status: 404 })
console.log(notFound.ok) // falsefunction mockResponse<T>(data: T, status = 200) {
return new Response(JSON.stringify(data), {
status,
headers: { 'Content-Type': 'application/json' }
})
}Understanding Response
The Response method in JavaScript creates a new Response object representing a response to a request. 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 new Response(body?, init?). It accepts 2 parameters: body, init. When called, it returns a new response object. Understanding when and how to use Response() helps you write more expressive, readable code.
Common use cases for Response include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like request-constructor, fetch-response-json, response-json-static, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Response 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
RequestCreates a new Request object representing a resource request that can be passed to fetch()
Response.prototype.jsonTakes a Response stream and reads it to completion, parsing the result as JSON
Response.jsonReturns a new Response object with a JSON-encoded body and the Content-Type header set to application/json
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.