InterviewHubPRO
Section Overview
Q42

Question 42 of 521

Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

8%
JavaScript Internals (521 Qs)•Part 1: Core Objects, Prototypes, Functions & Scope (1 - 53)

Why do you need a Cookie

Cookies are used to remember information about the user profile(such as username). It basically involves two steps,

  1. When a user visits a web page, the user profile can be stored in a cookie.
  2. Next time the user visits the page, the cookie remembers the user profile.

Practical Code Example

javascript
// Setting an HTTP-cookie or client cookie for session identification:
document.cookie = "sessionId=xyz987; Secure; SameSite=Strict; max-age=86400; path=/";

// Reading all available cookies
console.log(document.cookie); // "sessionId=xyz987"