JavaScript Cookies

Welcome to our guide on JavaScript cookies! In this article, we will explore what cookies are, how they work, and provide you with some practical examples to help you understand their usage in JavaScript.

What are Cookies?

Cookies are small pieces of data that websites store on a user’s computer. They are created by websites and sent to the user’s browser, where they are stored locally. Cookies are commonly used to remember user preferences, track user activity, and provide personalized experiences on websites.

How Do Cookies Work?

When a user visits a website, the server sends a response to the browser that includes a Set-Cookie header. This header contains the cookie data, which is then stored on the user’s computer. The browser will automatically include the cookie data in subsequent requests to the same website.

Each cookie has a name, value, and optional attributes such as expiration date, domain, and path. The name-value pair allows websites to store specific information about a user, such as their login status or shopping cart items. The attributes provide additional control over how the cookie is used and accessed.

JavaScript and Cookies

JavaScript provides a way to interact with cookies using the document.cookie property. This property allows you to read, write, and delete cookies from within your JavaScript code.

Example 1: Creating a Cookie

Let’s start with a simple example of creating a cookie using JavaScript:


document.cookie = "username=John Doe";

In this example, we set a cookie named “username” with the value “John Doe”. The cookie will be stored on the user’s computer until it expires or is manually deleted.

Example 2: Reading a Cookie

Once a cookie is created, you can access its value using the document.cookie property. Here’s an example:


let username = document.cookie;
console.log(username);

In this example, we retrieve the value of the “username” cookie and log it to the console. The output will be “username=John Doe”.

Example 3: Deleting a Cookie

To delete a cookie, you need to set its expiration date to a past date. Here’s an example:


document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

In this example, we set the expiration date of the “username” cookie to a past date, effectively deleting it from the user’s computer.

Working with Cookie Attributes

Cookies can have additional attributes that provide more control over their behavior. Let’s explore some common attributes:

Expiration Date

You can set an expiration date for a cookie using the “expires” attribute. This allows you to specify when the cookie should expire and be deleted. Here’s an example:


let expiryDate = new Date();
expiryDate.setDate(expiryDate.getDate() + 7); // Set the cookie to expire in 7 days
document.cookie = "username=John Doe; expires=" + expiryDate.toUTCString();

In this example, we set the “username” cookie to expire in 7 days from the current date.

Domain and Path

The “domain” and “path” attributes allow you to control the scope of a cookie. By default, cookies are associated with the domain and path of the current web page. However, you can specify a different domain or path to restrict or expand the cookie’s reach. Here’s an example:


document.cookie = "username=John Doe; domain=example.com; path=/";

In this example, we set the “username” cookie to be accessible on all subdomains of “example.com” and any path within those subdomains.

Conclusion

Cookies are a powerful tool for storing and retrieving user-specific information on websites. JavaScript provides a straightforward way to interact with cookies, allowing developers to create personalized experiences and enhance the functionality of their websites. By understanding how cookies work and their attributes, you can leverage them effectively in your JavaScript applications.

We hope this guide has provided you with a clear understanding of JavaScript cookies. Feel free to explore further and experiment with cookies to unlock their full potential in your web development projects.

Scroll to Top