Headers
Creates a new Headers object for managing HTTP request and response headers
Syntax
new Headers(init?)Parameters
| Parameter | Type | Description |
|---|---|---|
| init | HeadersInit | An object, array of key-value pairs, or existing Headers to initialize from |
Return Value
A new Headers object
Examples
const headers = new Headers()
headers.set('Content-Type', 'application/json')
headers.set('Authorization', 'Bearer token123')
console.log(headers.get('Content-Type'))const headers = new Headers({
'Content-Type': 'application/json',
'X-Custom': 'value'
})
console.log([...headers.entries()])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
Headers.prototype.getReturns the value of the specified header from a Headers object, or null if it does not exist
Headers.prototype.setSets a new value for an existing header inside a Headers object, or adds the header if it does not already exist
Headers.prototype.hasReturns whether a Headers object contains a certain header
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.