What are Cookies? Tell what they're for and how you used them.

👨‍💻 Frontend Developer 🟠 May come up 🎚️ Medium
#JavaScript #Browser #JS Basics

Brief Answer

Cookies are small notes that browser automatically sends to server with every request. Used for authentication, personalization and storing user settings. 🍪

// Setting cookie
document.cookie = 'username=John; path=/; expires=Fri, 31 Dec 2024 23:59:59 GMT';
 
// Reading cookie
console.log(document.cookie); // 'username=John'

Full Answer

Cookies are like notes you attach to a letter every time you write to a friend. Server can read these notes and remember who you are! 📝

What are Cookies

Cookies are small pieces of data (up to 4 KB) that:

  • Stored in browser
  • Automatically sent to server with every request
  • Have expiration date
// Simple cookie
document.cookie = 'theme=dark';

What Cookies are Used For

Authentication

// ✅ Save auth token
document.cookie = 'token=abc123; path=/; secure';
// Server will see this token in every request

Personalization

// ✅ Remember user language
document.cookie = 'language=en; path=/; max-age=31536000';
// Site will know language on next visit

Settings

// ✅ Save interface settings
document.cookie = 'fontSize=large; path=/; expires=Fri, 31 Dec 2024 23:59:59 GMT';

How Cookies Work

Setting

// Basic setting
document.cookie = 'name=value';
 
// With parameters
document.cookie = 'username=John; path=/; expires=Fri, 31 Dec 2024 23:59:59 GMT; secure';
  • path — paths where cookie is available
  • expires — expiration date
  • max-age — lifetime in seconds
  • secure — only over HTTPS
  • domain — domains where available

Reading

// Read all cookies
console.log(document.cookie); // 'username=John; theme=dark'
 
// Cookies come as string, need to parse

Common Mistakes

Storing large data

// ❌ Bad — cookies sent with every request
document.cookie = 'bigData=very_large_data'; // Slows site!
 
// ✅ Better — use localStorage
localStorage.setItem('bigData', 'very_large_data');

Not setting expiration

// ❌ Problem — cookie without expiration deleted when browser closes
document.cookie = 'temp=data';
 
// ✅ Good — set expiration
document.cookie = 'temp=data; max-age=3600'; // For one hour

Security

// ❌ Unsafe — cookie accessible through JavaScript
document.cookie = 'token=abc123';
 
// ✅ Safer — HttpOnly cookie set by server
// Set-Cookie: token=abc123; HttpOnly; Secure

When to Use Cookies

Good for

// ✅ Authentication (tokens, sessions)
// ✅ Personalization (language, theme)
// ✅ Tracking (analytics, advertising)

Not good for

// ❌ Large data (4 KB limit)
// ❌ Frequent changes (loads requests)
// ❌ Confidential data (visible in requests)

Simple Rules

  1. Small — maximum 4 KB 📏
  2. Automatic — sent with every request 🚀
  3. Expiration — set expires or max-age ⏰
  4. Security — use secure and HttpOnly 🔐
  5. Don’t overuse — localStorage for large data 🚫

Understanding Cookies helps store user data correctly and work with authentication! 💪


Want more articles to prepare for interviews? Subscribe to EasyAdvice, bookmark the site and improve yourself every day 💪