Headers.prototype.has
Returns whether a Headers object contains a certain header
Syntax
headers.has(name)Parameters
| Parameter | Type | Description |
|---|---|---|
| name | string | The name of the HTTP header to test for |
Return Value
A boolean indicating whether the header exists
Examples
const res = await fetch('/api/data')
console.log(res.headers.has('content-type')) // truefunction ensureJson(headers: Headers) {
if (!headers.has('Content-Type')) {
headers.set('Content-Type', 'application/json')
}
}const headers = new Headers({ 'Authorization': 'Bearer xyz' })
console.log(headers.has('Authorization')) // true
console.log(headers.has('X-Custom')) // falseUnderstanding Headers.prototype.has
The Headers.prototype.has method in JavaScript returns whether a Headers object contains a certain header. 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 headers.has(name). It accepts 1 parameter: name. When called, it returns a boolean indicating whether the header exists. Understanding when and how to use has() helps you write more expressive, readable code.
Common use cases for Headers.prototype.has 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-delete, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Headers.prototype.has 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.deleteDeletes a header from a Headers object
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.