All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Remove Cookie JavaScript

Last Updated : Mar 11, 2024

Remove Cookie JavaScript

In this tutorial we will show you the solution of remove cookie JavaScript, today we will understand how to remove cookie and its value in JavaScript.

The concept which is related to cookie or session is very interesting and say confusing concepts.

I am one from them who likes these concepts very much and always here to help others who have issues related to these concepts.

Today, we will understand how to remove cookie values in JavaScript.

Step By Step Guide On Remove Cookie JavaScript :-

Here, below we are going to show you how to remove cookies in JavaScript.

There are already some tutorials in our previous session in which we see how to set cookie, use or say access cookies in JavaScript.

If you have any issue regarding that then you can also visit to those tutorials. Now, below is the code given to remove cookies in JavaScript. Let us understand these first.

document.cookie = "cookieName=; expires = Thu, 01 Jan 1970 00:00:00 UTC; path=/;";

Hence, first main thing to delete a cookie is that the cookie must be saved.

If the cookie is not saved then there is no meaning to delete the cookie. To set a cookie the code used is as:

document.cookie = "cookieName = talkerscode; expires = monday, 18 Dec 2050 12:00:00 UTC; path=/";
  1. As we here see that we today write only JavaScript codes in our example.
  2. In previous tutorials, we already many times create a full code example in which we first create an html form with its basic structure. Inside that structure we use html, head, and title, body and script tags.
  3. In this example, we can also create a html page for use these JavaScript codes for better understanding. We can create a form, for example: a login page in which we store the value of username inside a cookie and then remove the cookie after some specific time by expiring it.
  4. There are many ways with help of which we can use cookies in html using JavaScript codes. Here, the main thing is that we have to remove the cookie which is already settled. To remove the cookie we use above codes. Let us understand these codes now.
  5. As above we see in the document.cookie, we use three things. First one is the name of cookie, another is expire time and last is path of cookie.
  6. The first thing that is name of cookie is mandatory. It helps to store cookie inside a variable. So, to remove cookie that is stored in that variable we just declare empty it. It means we reset the cookie using document.cookie and empty the variable in which we store our cookie. As a result our cookie gets removed.
  7. Now, next is expire time. This define the expire time of cookie. If we give it time that is past then today, then it means cookie get removed in past. This is another way to remove cookie.
  8. Last one is path, which is also mandatory. It helps to make us sure that we delete right cookie. We always have to specify the path, from which we have to remove cookie.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to remove cookie in JavaScript.

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪