Clue Mediator

How to set data in cookies using JavaScript

📅January 18, 2022

In this short article, we will show you how to set data in cookies using JavaScript.

Checkout more articles on JavaScript

Create a cookie

Use the `document.cookie` property to create a cookie using JavaScript.

document.cookie = "name=Clue Mediator";

Create a cookie and set an expiry date

You can set an expiry date and time (in UTC time) to make the cookie alive. By default, the cookie is deleted once the browser is closed.

document.cookie = "name=Clue Mediator; expires=Wed, 19 Jan 2022 12:00:00 UTC";

Cookie with a path parameter

By default, the cookie belongs to the current page. With a path parameter, you can tell the browser what path the cookie belongs to.

document.cookie = "name=Clue Mediator; expires=Wed, 19 Jan 2022 12:00:00 UTC; path=/";

In the upcoming article, we will see how to read the cookies from the browser using JavaScript.

That’s it for today.
Thank you for reading. Happy Coding..!! 🙂