Skip to content

JavaScript Cookie

A small JavaScript library for reading, writing and deleting browser cookies.js-cookie/js-cookie

Stars
22.6k
Forks
2k
Licence
MIT
Language
JavaScript
Last update
6 days ago
Type
Library

What it is

Js-cookie is a small JavaScript library that lets web pages set, read and delete cookies in the browser with simple function calls instead of working with the raw document.cookie string directly. It handles details such as encoding special characters and setting attributes like expiry, path, domain and secure. The README says it has no dependencies and is under 800 bytes gzipped.

  • Remember a user's language or theme choice across visits with Cookies.set and Cookies.get
  • Set a cookie that expires after a number of days using the expires option
  • Delete a cookie by passing the same path, domain, secure and sameSite values used to create it
  • Use noConflict to avoid clashing with an existing global Cookies variable on a third-party page

Great for

  • Front-end developers who need to store small pieces of data in cookies on a website
  • Teams that want a tiny, dependency-free cookie helper
  • Projects that must support both ES module bundlers and older CommonJS or script-tag setups

Not ideal for

  • Storing large amounts of data, since the README notes browsers may delete cookies that are too big or too numerous
  • Server-side cookie handling, since the library is described as client-side
  • Anyone who needs to read a cookie by specifying its domain or path, since the README says those attributes have no effect when reading
Step-by-step guide: how to use JavaScript CookieInstall, first run, examples, tips and FAQs

How to get it

  1. 1

    Install with a package manager

    Install from npm

    npm i js-cookie
  2. 2

    Run it

    • Install the package with npm i js-cookie or include it from the jsDelivr CDN
    • Import it with import Cookies from 'js-cookie'
    • Run Cookies.set('name', 'value') to create a cookie
    • Run Cookies.get('name') to read it back
  3. 3

    Get the most out of it

    • Use Cookies.withAttributes() to set default attributes for every call in your project
    • Pass expires as a Date instance for a specific expiry time instead of a number of days
    • Use custom converters to change the default encoding or decoding strategy
    • Read the project wiki for the FAQ on cookies being deleted and on expiring cookies in under a day

Downloads

Strengths

  • Very small size, stated as under 800 bytes gzipped in the README
  • No dependencies, and supports ES modules, AMD and CommonJS
  • Encodes special characters following RFC 6265 and allows custom converters

Trade-offs

  • Deleting cookies requires repeating the exact attributes used to set them, which is easy to get wrong
  • Cookie attributes must not come from untrusted input because of XSS risk, as the README warns
  • Browser behavior differs, such as Internet Explorer sending cookies to subdomains by default

Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.