What are cookies?

Chrome for DevelopersAbout 3 min readJul 30, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • Stateless Web: The web's inherent lack of memory between requests.
  • Cookies: Small text files stored by a user's web browser to remember information about them.
  • Headers: Extra information included in HTTP requests and responses.
  • Set-Cookie Header: An HTTP response header used by a server to instruct a browser to store a cookie.
  • Cookie Header: An HTTP request header used by a browser to send stored cookies back to the server.
  • First-Party Cookies: Cookies set by the website the user is currently visiting.
  • JavaScript Cookies: Cookies created using JavaScript code running in the browser.

The Problem of a Stateless Web

The web is inherently stateless, meaning each request is treated independently. The server has no built-in mechanism to remember previous interactions with a user. This simplicity is efficient but creates challenges for features like shopping carts or personalized experiences.

Cookies as a Solution

Cookies were invented to address the stateless nature of the web. They allow websites to store small pieces of data on a user's computer and retrieve them later.

Cookie Creation and Storage: A Step-by-Step Process

  1. Browser Request: A user's web browser requests a resource (e.g., an image file) from a web server (e.g., cats.example).
  2. Server Response with Set-Cookie Header: The web server responds with the requested file and includes a Set-Cookie header. For example: Set-Cookie: cat=tabby.
  3. Browser Stores Cookie: The browser processes the Set-Cookie header and stores the cookie (name: cat, value: tabby) in a file.

Cookie Transmission: Sending Data Back to the Server

  1. Subsequent Browser Request: When the browser makes another request to the same web server (cats.example), it automatically includes a Cookie header.
  2. Cookie Header: The Cookie header contains the name-value pairs of the stored cookies. For example: Cookie: cat=tabby.
  3. Server Processes Cookie: The web server receives the request with the Cookie header and can use the information (e.g., cat=tabby) to personalize the user's experience, such as avoiding displaying the same tabby cat image again.

Cookie Scope: Site Specificity

Cookies are only sent to the domain that set them. A cookie set by cats.example will not be sent to dogs.example. This prevents websites from accessing cookies set by other sites.

Server-Side vs. Client-Side Cookie Creation

Cookies can be set by code running on the web server (using the Set-Cookie header) or by JavaScript code running in the user's web browser.

Conclusion

Cookies provide a mechanism to overcome the stateless nature of the web, enabling features like shopping carts and personalized experiences. The process involves the server sending a Set-Cookie header in its response, the browser storing the cookie, and the browser subsequently sending the cookie back to the server in the Cookie header with each request to that domain. Cookies are site-specific and can be created both server-side and client-side.

AI summaries can miss context or contain errors. Check important details against the original video.

MAKE IT YOURS

Read. Remember. Reuse.

Free tools

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.