Response

Response

Creates a new Response object representing a response to a request

Syntax

JavaScript
new Response(body?, init?)

Parameters

ParameterTypeDescription
bodyBodyInit | nullThe body of the response (Blob, ArrayBuffer, string, ReadableStream, etc.)
initResponseInitOptions: status, statusText, headers

Return Value

A new Response object

Examples

Basic Usage
const response = new Response(JSON.stringify({ ok: true }), {
  status: 200,
  headers: { 'Content-Type': 'application/json' }
})
console.log(response.status) // 200
Practical Example
const notFound = new Response('Not Found', { status: 404 })
console.log(notFound.ok) // false
Advanced Usage
function 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

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.