All TalkersCode Topics

Follow TalkersCode On Social Media

How To Set Cookie In JavaScript

Last Updated : Jan 1, 2023

How To Set Cookie In JavaScript

In this tutorial we will show you the solution of how to set cookie in JavaScript, cookie we already know it is data stored in small text files, on our computer and those data only available till chrome window get closed.

Here we created and set as cookie on the browser for a particular limit of day and time so it will available until it reach defined time and date.

So we can use those data for any purpose incase when we need.

Step By Step Guide On How To Set Cookie In JavaScript :-

Here we defined one variable ‘username’ with value initialization and we defined function ‘setcookie’ for sets date and time of expiry.

This function defined with parameters ‘cookie_name,cookie_value,expiry_date’ and within function we created date object for sets date, time after that we finally sets cookie by using ‘document.cookie’ object service.

The document property cookie lets you read and write cookies associated with the document.

<!DOCTYPE html>
        <title>Set Cookie</title>
            let username='MaxBrown';
           function setcookie(cname,cvalue,expdate){
            let date=new Date();
            const expires="expires="+date.toUTCString();
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. In <script> tag we defined variable ‘username’ with value ‘MaxBrown’ after that setcookie() function defined with parameters ‘cname,cvalue,expdate’ and within function we created date object and stored to variable ‘date’.
  7. Then setTime() method used to sets expiry date and ‘getTime()’ function used to sets expiry time. Both method using to calculate expiry date with defined date and appends to date object.
  8. After that ‘toUTCString()’ method used to change date format to UTC string format then finally we sets cookie on the browser with passed values.
  9. Finally we defined function call setcookie() with passed parameters ‘’username’,’MaxBrown’,30’.
  10. In browser for open javascript console use the shortcut ‘ctrl+shift+j’ then console panel will open at right hand side there we can see our cookie.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion we are able to know how to set cookie in javascript.

When we execute our program on browser we need to confirm one thing is our program should be hosted on internet then we have to use shortcut ‘ctrl+shift+j’.

So console panel will open with our defined cookie ‘username:MaxBrown’ it will available till date 30th current time of when we defined.

We can also set or get cookie value at console panel because we had access of write or read any script code on console panel and we will see about them in future.

I hope this tutorial on how to set cookie in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Latest Tutorials