This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
Note: This feature is available in Web Workers.
The Headers.forEach() method executes a callback function once per each key/value pair in the Headers object.
forEach(callbackFn) forEach(callbackFn, thisArg)
callbackFnFunction to execute for each entry in the map. It takes the following arguments:
thisArg OptionalValue to use as this when executing callback.
The Headers.forEach() method executes the provided callback once for each key of the Headers which actually exist. It is not invoked for keys which have been deleted. However, it is executed for keys which are present but have the value undefined.
The following code logs a line for each key/value in the myHeaders object.
// Create a new test Headers object
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Cookie", "This is a demo cookie");
myHeaders.append("compression", "gzip");
// Display the key/value pairs
myHeaders.forEach((value, key) => {
console.log(`${key} ==> ${value}`);
});
The result is:
compression ==> gzip content-type ==> application/json cookie ==> This is a demo cookie
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
forEach |
42 | 14 | 47 | 29 | 10.1 | 42 | 47 | 29 | 10.3 | 4.0 | 42 | 10.3 |
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Headers/forEach