Maharashtra's Most Trusted

with more than 0 members

Chapter 4 - Cookies & Browser Data

For MSBTE Diploma CO / IT / AIML Branch

Define Cookies -: 

In JavaScript, cookies are used to store small pieces of data, such as user preferences, session information, or tracking data. These cookies are stored as key-value pairs in the browser and are sent back to the server on each request.

Types of Cookies -: 

In JavaScript, cookies can be classified into different types based on their lifespan, security, and scope. Here’s a breakdown of the types of cookies:

1. Session Cookies
  • Description: These cookies are stored in memory and last only for the duration of the user’s browsing session. They are deleted once the user closes the browser.
  • Use Case: Commonly used for temporary data, such as keeping a user logged in while navigating a website.
  • Example: Shopping cart data that is cleared after the user leaves the site.
2. Persistent Cookies
  • Description: These cookies have an expiration date or a maximum age set. They are stored on the user’s device until they expire or are manually deleted by the user or application.
  • Use Case: Useful for remembering login credentials, user preferences, or tracking user behavior over a long period.
  • Example: Storing a user’s preferred language for future visits.
Creating Cookie -: 

To create a cookie, you can assign a string to document.cookie. The string must include a name-value pair, and you can optionally add other attributes like expires, path, etc.

Reading Cookie -: 

To read a cookie, you can use the document.cookie property. This returns all cookies in a single string. You may need to parse it to get a specific cookie.

Deleting Cookie -: 

To delete a cookie, set its expires attribute to a past date.

Cookie Complete Example -: 

end of part 1 -------->

Explore Other Chapters