Headers

Headers

Creates a new Headers object for managing HTTP request and response headers

Syntax

JavaScript
new Headers(init?)

Parameters

ParameterTypeDescription
initHeadersInitAn object, array of key-value pairs, or existing Headers to initialize from

Return Value

A new Headers object

Examples

Basic Usage
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.set('Authorization', 'Bearer token123')
console.log(headers.get('Content-Type'))
Practical Example
const headers = new Headers({
  'Content-Type': 'application/json',
  'X-Custom': 'value'
})
console.log([...headers.entries()])
Advanced Usage
function createAuthHeaders(token: string) {
  return new Headers({
    'Authorization': `Bearer ${token}`,
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  })
}

Understanding Headers

The Headers method in JavaScript creates a new Headers object for managing HTTP request and response headers. It belongs to the Headers object and is one of the most widely used methods for working with headers values in modern JavaScript and TypeScript applications.

The method signature is new Headers(init?). It accepts 1 parameter: init. When called, it returns a new headers object. Understanding when and how to use Headers() helps you write more expressive, readable code.

Common use cases for Headers include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like fetch-headers-get, fetch-headers-set, fetch-headers-has, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Headers 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 Headers Methods

Other methods in the Headers object

Related Tools

More Headers Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.