Headers

Headers.prototype.has

Returns whether a Headers object contains a certain header

Syntax

JavaScript
headers.has(name)

Parameters

ParameterTypeDescription
namestringThe name of the HTTP header to test for

Return Value

A boolean indicating whether the header exists

Examples

Basic Usage
const res = await fetch('/api/data')
console.log(res.headers.has('content-type')) // true
Practical Example
function ensureJson(headers: Headers) {
  if (!headers.has('Content-Type')) {
    headers.set('Content-Type', 'application/json')
  }
}
Advanced Usage
const headers = new Headers({ 'Authorization': 'Bearer xyz' })
console.log(headers.has('Authorization')) // true
console.log(headers.has('X-Custom')) // false

Understanding 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

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.