How to create cookie with the help of JavaScript ? Last Updated : 10 Feb, 2022 Comments Improve Suggest changes Like Article Like Report A cookie is an important tool as it allows you to store the user information as a name-value pair separated by a semi-colon in a string format. If we save a cookie in our browser then we can log in directly to the browser because it saves the user information. Approach: When a user sends the request to the server then the cookie tells that this user is already logged in your browser and the server recognizes the user and allows the user to log in. We can apply various operations on cookie-like create, delete, read, add an expiry date to it so that users can never be logged in after a specific time. A cookie is created by the document.cookie keyword as shown below. Example: In the below example, we will take input from the user as a name for the cookie and store it in cookievalue. Then cookievalue is stored in string format adding the 'name' attribute and then the cookie is created in the browser. HTML <!DOCTYPE html> <html> <body> <form name="myform" action=""> Enter name: <input type="text" name="customer" /> <input type="button" value="Set Cookie" onclick="WriteCookie();" /> </form> <script type="text/javascript"> function WriteCookie() { if (document.myform.customer.value == "") { alert("Please enter value"); return; } cookievalue = escape( document.myform.customer.value) + ";"; document.cookie = "name=" + cookievalue; document.write("Setting Cookies : " + "name=" + cookievalue); } </script> </body> </html Output: Adding an expiry date (in UTC): We can add expire date for the cookie to ensure that after that time the cookie will no longer be in use. Syntax: document.cookie = "username=geeksforgeeks; expires=Sun, 16 JAN 2022 12:00:00 UTC";Deleting the Cookie: When you want to delete the cookie just simply set the expires parameter and leave the username blank. document.cookie = "username=; expires=Sun, 16 JAN 2022 12:00:00 UTC"; Comment More infoAdvertise with us Next Article How to create cookie with the help of JavaScript ? rohanmittal1366 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to list all the cookies of the current page using JavaScript ? Cookies are small pieces of data stored by websites on your browser to remember information about your visit, such as login status, preferences, or other settings. In JavaScript, you can easily retrieve cookies stored by the current domain using document.cookie. However, due to security reasons, you 3 min read How to Get Cookie by Name in JavaScript? Getting a specific name in JavaScript involves parsing the document's cookie string contains all cookies in a single string separated by semicolons and space. The goal is to extract the value of a specific cookie by the name given. The Cookie is defined as a small piece of data that is stored on the 4 min read How to Set & Retrieve Cookies using JavaScript ? In JavaScript, setting and retrieving the cookies is an important task for assessing small pieces of data on a user's browser. This cookie can be useful for storing user preferences, session information, and many more tasks. Table of Content Using document.cookieUsing js-cookie libraryUsing document 2 min read How to create Cookie Consent Box using HTML CSS and JavaScript ? In this article, we will see how cookies are stored using the cookie consent box using HTML, CSS, & JavaScript.Cookies are small pieces of data that are stored on a user's device while browsing the website. Cookies are used to store information for faster access to data and provide a better user 4 min read How to Set Cookies Session per Visitor in JavaScript? Managing session cookies in JavaScript is essential for web developers to maintain user state and preferences across multiple sessions. The document. cookie property provides a basic mechanism for cookie management, utilizing JavaScript libraries or frameworks can offer enhanced security and flexibi 4 min read Like