Creating a Cookie — JS Technical Interview Questions & Answers

Writing to document.cookie with an expiry.

Creating a Cookie

  • Writing to document.cookie with an expiry.
  • Creating a Cookie (js cookies create) is a JS topic. In plain words you use it for remember theme in the browser. Don’t start with a slogan — start with that picture.
  • Smallest example: localStorage.setItem('theme', 'dark'). Type it, run it, and say what you see. If you can do that from memory, you know Creating a Cookie (js cookies create).
  • From the example next to this theory: Writing to document.
  • Trap: storing passwords in localStorage. Fix that before you talk about advanced JS.
  • Viva: what is Creating a Cookie (js cookies create)? Then show localStorage.setItem('theme', 'dark'). Then name the trap.

Technical Interview Tip: What is Creating a Cookie (js cookies create)? Show this: localStorage.setItem('theme', 'dark'). Trap: storing passwords in localStorage.

Code Examples for Creating a Cookie

Creating a Cookie — working example

// Browser:
// document.cookie = "theme=dark; Max-Age=86400; Path=/; SameSite=Lax";
// localStorage.setItem("theme", "dark");
// sessionStorage.setItem("draft", "hello");

// Runnable stand-in:
const jar = { theme: "dark" };
const store = new Map([["theme", "dark"]]);
console.log(jar.theme, store.get("theme"));
store.delete("theme");
console.log(store.has("theme"));

Creating a Cookie — cookies go to the server; localStorage persists; sessionStorage lasts for the tab.

How to Explain Creating a Cookie to an Interviewer

What is Creating a Cookie (js cookies create)?

Used for remember theme the browser Not just the heading

Give one small example of Creating a Cookie (js cookies create).

localStorage.setItem('theme', 'dark') Writing to document

What mistake do beginners make with Creating a Cookie (js cookies create)?

storing passwords in localStorage Show the fix

Where do you use Creating a Cookie (js cookies create)?

the browser remember theme