Cookies in JavaScript are essential for creating dynamic and interactive web applications. They allow data to be stored on the client side, enabling features like user preferences, authentication, and session management. Let's explore an example of using cookies in JavaScript.

First, let's understand how to set, get, and delete cookies using JavaScript.

Setting, Getting, and Deleting Cookies
To work with cookies, we'll use functions that follow these conventions:

- Setting a cookie: Set the cookie name, value, and options (expiration time, path, etc.).
- Getting a cookie: Retrieve the value of a specific cookie by its name.
- Deleting a cookie: Remove a cookie by setting its expiration date to a past date.
Setting a Cookie

Here's a function to set a cookie with an optional expiration time:
function setCookie(key, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
let expires = "expires="+ date.toUTCString();
document.cookie = key + "=" + (value || "") + ";" + expires + ";path=/";
}
To set a cookie named "username" with a value of "John Doe" and an expiration time of 1 day:
setCookie('username', 'John Doe', 1);

Getting a Cookie
To retrieve the value of a cookie by its name:
function getCookie(key) {
let name = key + "=";
let decode = decodeURIComponent(document.cookie);
let ca = decode.split(';');
for(let i = 0; i
To get the value of the "username" cookie:

getCookie('username');
Deleting a Cookie




















To remove a cookie, set its expiration date to the past:
function deleteCookie(key) {
document.cookie = key + '=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/';
}
To delete the "username" cookie:
deleteCookie('username');
Cookies in Practice: Remembering User Preferences
Cookies can store user preferences, like dark mode or theme choices. Let's create an example of changing the website's theme based on user preference stored in a cookie.
Changing the Theme
First, we'll create an element with an ID "theme-toggle" in our HTML:
<button id="theme-toggle">Toggle Dark Mode</button>
Now, we'll write JavaScript to toggle the theme and set/get the user's preference:
Javascript
let themeToggle = document.getElementById('theme-toggle');
let body = document.body;
let themeCookie = "theme";
function toggleTheme() {
if (getCookie(themeCookie) === 'dark') {
setCookie(themeCookie, 'light', 30);
body.classList.remove('dark-mode');
} else {
setCookie(themeCookie, 'dark', 30);
body.classList.add('dark-mode');
}
}
themeToggle.addEventListener('click', toggleTheme);
if (getCookie(themeCookie) === 'dark') {
body.classList.add('dark-mode');
}
This code sets up an event listener for the "theme-toggle" button that switches the theme and sets/updates the cookie. The initial page load checks the cookie and applies the user's preferred theme.
Cookies are an essential part of building interactive and personalized web experiences. They enable features like user preferences, authentication, and session management across different requests. In this example, we've demonstrated how to set, get, and delete cookies, and also how to use cookies to store and retrieve user preferences. By leveraging cookies, we can create more engaging and intuitive web applications.