Headers

Headers.prototype.keys

Returns an iterator allowing you to go through all keys of the key/value pairs contained in the Headers object

Syntax

JavaScript
headers.keys()

Return Value

An iterator of header name strings

Examples

Basic Usage
const res = await fetch('/api/data')
const headerNames = [...res.headers.keys()]
console.log(headerNames)
Practical Example
function hasAllHeaders(headers: Headers, required: string[]): boolean {
  const keys = new Set([...headers.keys()])
  return required.every(h => keys.has(h.toLowerCase()))
}
Advanced Usage
const headers = new Headers({ 'Content-Type': 'text/html', 'X-Id': '1' })
console.log([...headers.keys()]) // ['content-type', 'x-id']

Understanding Headers.prototype.keys

The Headers.prototype.keys method in JavaScript returns an iterator allowing you to go through all keys of the key/value pairs contained in the Headers object. 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.keys(). When called, it returns an iterator of header name strings. Understanding when and how to use keys() helps you write more expressive, readable code.

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

Browser support for Headers.prototype.keys 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.