# Get the value of a cookie

Find one cookie by name in a Cookie header string.

Canonical: https://1loc.dev/misc/get-the-value-of-a-cookie/
Version: 1.0.0
Runtime: JavaScript · Node.js 22 / modern browsers
Verification: Local fixtures; no external services
Updated: 2026-10-28

## Implementation

```javascript
function getCookie({ cookie, name }) { const match = `; ${cookie}`.split(`; ${name}=`); return match.length > 1 ? match.pop().split(';').shift() : null; }
```

## Input

```json
{
  "cookie": "theme=dark; session=abc123",
  "name": "session"
}
```

## Expected output

```json
"abc123"
```

## Edge case

Input:
```json
{
  "cookie": "theme=dark",
  "name": "session"
}
```
Output:
```json
null
```

## Limits

- A missing cookie returns null; the original returned the whole cookie string for an unknown name.
- In a browser pass document.cookie; on a server, the request’s Cookie header.

The lab validates types and size before calling this function. Add equivalent validation at your own trust boundary.

## Source and license

Original: https://github.com/phuocng/1loc/blob/14b481cf60dff1620ab1bb99a43b1f68068cdd9b/contents/get-the-value-of-a-cookie.mdx
Author: Nguyen Huu Phuoc and original contributors
Contributors: See original repository history
Source commit: 14b481cf60dff1620ab1bb99a43b1f68068cdd9b
Copyright (c) 2020 phuoc-ng
License: MIT — https://1loc.dev/licenses/
Maintainer: Oveyon
Modifications: Takes the cookie string as input and returns null for a missing name; the original returned the whole string. Original MDX retained as inert source evidence.
