All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Delete Session Cookie

Last Updated : Mar 11, 2024

JavaScript Delete Session Cookie

In this tutorial on JavaScript delete session cookie, usually in script we had two methods for delete cookie session either by expire date or by max-age here we are going to use second type.

Using max-age property we can sets limit life time for a cookie so here we set it value as simply ‘0’ it will immediately get deleted at the time of creating and if we using first type then we can set expires date as much days as we want.

Step By Step Guide On JavaScript Delete Session Cookie :-

Here we defined two input elements with id’s ‘btn1,btn2’ and two different values of onclick event ‘setcooke(),getcooke()’ for gets loaded when user clicks on it.

In script we defined those two methods declared at input tags, in setcooke() method we sets cookie Name as ‘John’ with ‘Max-age’ value as ‘0’ so its life time limit is ‘0’.

In getcooke() method we checking cookie length using if condition because if cookies are present then its length definitely greater than ‘0’ that’s way we provide condition like it otherwise we can say there no more cookie.

<!DOCTYPE html>
        <title>Delete Session Cookie</title>
        <input type="button" name="" id="btn1" onclick="setcooke()" value="SetCookie">
        <input type="button" name="" id="btn2" onclick="getcooke()" value="GetCookie">
            function setcooke(){
            function getcooke(){
                if(document.cookie.length != 0){
                    console.log("Cookie not available");
  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 contain 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. Here we defined two input elements with id attributes ‘btn1,btn2’ for generate defined setcooke(), getcooke() methods respectively depends on user response.
  7. In script we defined setcooke() method within that we sets cookie Name as ‘John’, ‘Max-age’ value is ‘0’. So it will saying this cookie life span is ‘0’ limit when we creating it will get deleted immediately.
  8. In getcooke() method using if condition we checks cookie length for identifies whether cookies are present or not. If cookie length had greater than ‘0’ then there definitely cookies present otherwise we could say there is no more cookies.
  9. Respective message will printed on console you can check it on console in web browser.
  10. 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 now we are able to know how to delete session cookie using javascript.

When we executes program on browser we can see two buttons namely ‘SetCookie, GetCookie’ when user clicks on them respective methods will loaded then for seeing result on console we have to use shortcut ‘ctrl+shift+j’ then at right hand side console panel will open with result message ‘Cookie not available’ because we deleted when we created cookie by Max-age.

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

Author Image About Dikshita

Passionate Electronics and Communication Engineering student with expertise in web development (HTML, CSS, JS,PHP, Bootstrap, React.js) and content writing. Eager problem solver and tech enthusiast, adept at creating engaging web experiences.

Follow Dikshita On Linkedin 🡪