Dive into the world of web development with us today as we explore JavaScript cookies – a fundamental aspect of client-side web storage. Widely used to maintain stateful information across a single session, cookies enhance user experience and facilitate features like login authentication, session management, and personalized settings. Let's delve into the intricacies of JavaScript cookies, guided by the clear and concise tutorials provided by W3Schools.

Before we embark on our learning journey, let's briefly understand what cookies are and their significance. Cookies are small text files stored on the user's device by the web browser. They contain key-value pairs, with keys serving as names and values storing the actual data. These tiny powerhouses enable web applications to recall and manipulate data, even as users navigate between pages.

Setting Cookies with JavaScript
W3Schools offers a simple and intuitive approach to setting cookies using JavaScript. The `document.cookie` property allows us to create, read, and modify cookies directly in our code.

To set a cookie, we use the following syntax: `document.cookie = "key=value";`. Here's a basic example from W3Schools:
document.cookie = "username=John Doe";
Assigning Cookie Expiration Date

By default, cookies expire when the browsing session ends. To persist cookies across sessions, we assign an expiration date. The `expires` attribute accepts a date as a string in the format `"YYYY-MM-DD HH:MM:SS"`. Here's how you can set an expiration date:
let d = new Date();
d.setTime(d.getTime() + (30*24*60*60*1000));
document.cookie = "username=John Doe; expires=" + d.toUTCString();
In the above example, the cookie will expire in 30 days.
Assigning Cookie Domain and Path

Cookies can be set to apply only to a specific domain or path. The `domain` and `path` attributes allow us to define these constraints:
document.cookie = "username=John Doe; domain=.w3schools.com; path=/";
The cookie above will only be accessible within the 'w3schools.com' domain and its subdomains, like 'www.w3schools.com'.
Reading Cookies with JavaScript

Now that we know how to set cookies let's explore how to read and retrieve their values. The `document.cookie` property returns a string containing all cookies stored for a particular domain. This string is in the format 'key=value; key2=value2; key3=value3;', making it easy to parse and extract individual cookies.
Parsing Cookie String




















To extract individual cookies, we can use the `Document cookie` property combined with the `split()`, `pop()`, and `split('=')` methods. Here's a simple function that retrieves all cookies and their values:
function getCookies() {
let cookies = document.cookie.split(';');
for (let i = 0; i < cookies.length; i++) {
let cookie = cookies[i].split('=');
console.log(cookie[0].trim() + ' = ' + cookie[1]);
}
}
The `getCookies()` function will log all cookies and their respective values to the console.
Retrieving a Specific Cookie
To retrieve a specific cookie, we search for the target key within the cookie string. W3Schools provides an example function `getCookie("keyname")` to achieve this:
function getCookie(cname) {
let name = cname + "=";
let decodedCookie = decodeURIComponent(document.cookie);
let ca = decodedCookie.split(';');
for(let i = 0; i
This function returns the value of the specified cookie, or an empty string if the cookie doesn't exist.
Deleting Cookies with JavaScript
To delete a cookie, we set its expiration date to a past date. This essentially 'expires' the cookie, removing it from the user's device.
Deleting a Specific Cookie
Here's how to delete a cookie with a given name, using W3Schools' example function:
function deleteCookie(cname) {
document.cookie = cname + '=; max-age=0';
}
The `deleteCookie('keyname')` method sets the cookie's expiration date to the past, effectively deleting it.
This exploration of JavaScript cookies has equipped us with the essential knowledge and tools to manipulate cookies in our web applications. Utilizing W3Schools' clear and concise tutorials, we'veLearn how to set, read, and delete cookies, and even assigned expiration dates, domains, and paths. Embrace these newfound skills to enhance user experience and functionality in your projects.