# JavaScript Examples - Extract Cookie Value

Read the value of a specific cookie by name directly on Azion's global network and use it to shape the response. This is helpful for A/B testing, personalization, or any logic that depends on data your application already stores in the visitor's cookies.

```js
const COOKIE_NAME = "hubspotutk"

function getCookie(request, name) {
  let result = ""
  const cookieString = request.headers.get("Cookie")
  if (cookieString) {
    const cookies = cookieString.split(";")
    cookies.forEach(cookie => {
      const cookiePair = cookie.split("=", 2)
      const cookieName = cookiePair[0].trim()
      if (cookieName === name) {
        const cookieVal = cookiePair[1]
        result = cookieVal
      }
    })
  }
  return result
}

function handleRequest(request) {
  const cookie = getCookie(request, COOKIE_NAME)
  if (cookie) {
    return new Response(cookie)
  }
  return new Response("No cookie with name: " + COOKIE_NAME)
}

addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request))
})
```

## How it works

The helper `getCookie` reads the `Cookie` header with `request.headers.get("Cookie")`, splits it on `;` into individual pairs, and splits each pair on `=` to compare the cookie name against the one you want. When a match is found, it returns that cookie's value. The `fetch` handler then calls `getCookie` and uses `event.respondWith()` with a new `Response`, returning either the cookie value or a message noting the cookie was not present.

## Related resources

- [JavaScript examples](/en/documentation/devtools/javascript-examples/)
- [Runtime APIs](/en/documentation/products/applications/functions/runtime-apis/javascript/fetch-event/)