Skip to content
beginnerPhase 29 · Web Foundations

Cookies

Understand how cookies work, their properties, and security considerations.

30m
0 problems
Topic Progress0%

What are Cookies

Cookies are small pieces of data stored in the browser, sent with every HTTP request to the same domain.

How Cookies Work

1. Server sends Set-Cookie header
   HTTP/1.1 200 OK
   Set-Cookie: session=abc123; Path=/; HttpOnly

2. Browser stores cookie
   ┌─────────────────────────────────────┐
   │ Domain: example.com                 │
   │ Name: session                       │
   │ Value: abc123                       │
   │ Path: /                             │
   │ HttpOnly: true                      │
   └─────────────────────────────────────┘

3. Browser sends cookie with subsequent requests
   GET /dashboard HTTP/1.1
   Host: example.com
   Cookie: session=abc123

Cookie Types

Type Purpose Lifetime
Session Temporary, lost when browser closes Session
Persistent Survives browser restart Explicit expiry
First-party Set by the domain you're visiting Varies
Third-party Set by a different domain (ads, tracking) Varies

Cookie Uses

  • Authentication: Maintain login state
  • Preferences: Theme, language settings
  • Tracking: User behavior analytics
  • Shopping Cart: E-commerce cart contents
  • Session Management: Server-side session ID

Cookie Limitations

  • Size: ~4KB per cookie
  • Quantity: ~50 per domain
  • Sent with every request: Adds overhead
  • Visible to JavaScript (unless HttpOnly)

JavaScript Access

// Read cookies
console.log(document.cookie); // "session=abc123; theme=dark"

// Set cookie
document.cookie = "theme=dark; path=/; max-age=86400";

// Delete cookie (set expiry to past)
document.cookie = "theme=; path=/; max-age=0";

Practice Problems

0/3solved
Build Cookies Component

Create a reusable React component implementing Cookies. Include proper state management and accessibility.

Solution
// Production-ready component with:
// - Proper TypeScript types
// - Accessibility (ARIA)
// - Error boundaries
// - Loading states
// - Memoization where needed
Cookies Testing

Write unit and integration tests for Cookies using React Testing Library.

Solution
// Test coverage:
// 1. Rendering tests
// 2. Interaction tests
// 3. Edge case tests
// 4. Accessibility tests
Cookies Performance

Optimize Cookies for performance. Consider memoization, code splitting, and bundle size.

Solution
// Optimization techniques:
// 1. React.memo / useMemo / useCallback
// 2. Code splitting with lazy()
// 3. Virtual scrolling for lists
// 4. Image lazy loading
// 5. Bundle analysis

Quiz

1. What does the HttpOnly flag do?

Question 1 options

2. What is the maximum size of a cookie?

Question 2 options

3. Which SameSite value provides the strongest CSRF protection?

Question 3 options

4. What is the difference between Max-Age and Expires?

Question 4 options

Flashcards

Question

What are cookies?

Answer

Small pieces of data stored in the browser, sent with every HTTP request to the same domain. Used for authentication, preferences, and tracking.

Question

What does Secure flag do?

Answer

Ensures cookie is only sent over HTTPS connections, preventing interception over HTTP.

Question

What is the difference between session and persistent cookies?

Answer

Session cookies are deleted when the browser closes. Persistent cookies have an expiry date and survive browser restarts.

Question

How do you delete a cookie?

Answer

Set its value to empty and Max-Age to 0: document.cookie = 'name=; Max-Age=0; path=/'

Question

What is Cookies?

Answer

Cookies is a key concept in frontend development.

Revision Notes

Key Takeaways

  • 1.Cookies are sent with every HTTP request to the same domain
  • 2.Use Secure, HttpOnly, and SameSite for security
  • 3.Session cookies expire when browser closes, persistent cookies have expiry dates
  • 4.SameSite attribute protects against CSRF attacks
  • 5.Keep cookie sizes small to minimize request overhead

Interview Tips

  • Explain how cookies enable authentication
  • Know the security implications of each cookie flag
  • Understand SameSite and CSRF protection
  • Know when to use cookies vs localStorage vs sessions

Cheat Sheet

Cookies Cheat Sheet

Cookie Properties:

  • Name/Value: Cookie data
  • Domain: Which domains receive it
  • Path: URL path scope
  • Max-Age/Expires: Lifetime
  • Secure: HTTPS only
  • HttpOnly: No JS access
  • SameSite: CSRF protection

Security Best Practices:

  • Use Secure + HttpOnly for auth cookies
  • Use SameSite=Strict for sensitive data
  • Set short expiry for session cookies
  • Always use HTTPS

SameSite Values:

  • Strict: Never cross-site
  • Lax: Top-level GET only (default)
  • None: All requests (needs Secure)

Limitations:

  • ~4KB per cookie
  • ~50 per domain
  • Sent with every request