8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Home JavaScript HTML CSS Bootstrap jQuery Node.js PHP
https://www.javatpoint.com/javascript-deleting-cookies 1/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Loose Leaf Refill Binder Notebook
A4/B5/A5, Blank/Line/Grid/Cornel
AliExpress
Deleting a Cookie in JavaScript
In the previous section, we learned the different ways to set and update a cookie in JavaScript. Apart
from that, JavaScript also allows us to delete a cookie. Here, we see all the possible ways to delete a
cookie.
Different ways to delete a Cookie
These are the following ways to delete a cookie:
A cookie can be deleted by using expire attribute.
A cookie can also be deleted by using max-age attribute.
We can delete a cookie explicitly, by using a web browser.
Examples to delete a Cookie
Example 1
In this example, we use expire attribute to delete a cookie by providing expiry date (i.e. any past
date) to it.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="name=Martin Roy; expires=Sun, 20 Aug 2000 12:00:00 UTC";
https://www.javatpoint.com/javascript-deleting-cookies 2/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not avaliable");
}
}
</script>
</body>
</html>
Example 2
In this example, we use max-age attribute to delete a cookie by providing zero or negative number
(that represents seconds) to it.
History of the World Map by Map
Ultimate guide to history throughout the ages!
Amazon
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="name=Martin Roy;max-age=0";
https://www.javatpoint.com/javascript-deleting-cookies 3/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not avaliable");
}
}
</script>
</body>
</html>
Example 3
Let's see an example to set, get and delete multiple cookies.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie1" onclick="setCookie1()">
<input type="button" value="Get Cookie1" onclick="getCookie1()">
<input type="button" value="Delete Cookie1" onclick="deleteCookie1()">
<br>
<input type="button" value="Set Cookie2" onclick="setCookie2()">
<input type="button" value="Get Cookie2" onclick="getCookie2()">
<input type="button" value="Delete Cookie2" onclick="deleteCookie2()">
<br>
<input type="button" value="Display all cookies" onclick="displayCookie()">
https://www.javatpoint.com/javascript-deleting-cookies 4/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
<script>
function setCookie1()
{
document.cookie="name=Martin Roy";
cookie1= document.cookie;
}
function setCookie2()
{
document.cookie="name=Duke William";
cookie2= document.cookie;
}
function getCookie1()
{
if(cookie1.length!=0)
{
alert(cookie1);
}
else
{
alert("Cookie not available");
}
}
function getCookie2()
{
if(cookie2.length!=0)
{
alert(cookie2);
}
else
{
alert("Cookie not available");
}
}
function deleteCookie1()
{
https://www.javatpoint.com/javascript-deleting-cookies 5/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
document.cookie=cookie1+";max-age=0";
cookie1=document.cookie;
alert("Cookie1 is deleted");
}
function deleteCookie2()
{
document.cookie=cookie2+";max-age=0";
cookie2=document.cookie;
alert("Cookie2 is deleted");
}
function displayCookie()
{
if(cookie1!=0&&cookie2!=0)
{
alert(cookie1+" "+cookie2);
}
else if(cookie1!=0)
{
alert(cookie1);
}
else if(cookie2!=0)
{
alert(cookie2);
}
else{
alert("Cookie not available");
}
</script>
</body>
</html>
https://www.javatpoint.com/javascript-deleting-cookies 6/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Example 4
Let's see an example to delete a cookie explicitly.
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="Set Cookie" onclick="setCookie()">
<input type="button" value="Get Cookie" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="name=Martin Roy";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("Cookie not avaliable");
}
}
</script>
</body>
</html>
After clicking Set Cookie once, whenever we click Get Cookie, the cookies key and value is
displayed on the screen.
https://www.javatpoint.com/javascript-deleting-cookies 7/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
To delete a cookie explicitly, follow the following steps:
Open Mozilla Firefox.
Click Open menu - Library - History - Clear Recent History - Details.
Here we can see a Cookies checkbox which is already marked. Now, click Clear Now to
delete the cookies explicitly.
Now, on clicking Get Cookie, the below dialog box appears.
https://www.javatpoint.com/javascript-deleting-cookies 8/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Here, we can see that the cookies are deleted.
← Prev Next →
https://www.javatpoint.com/javascript-deleting-cookies 9/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Youtube For Videos Join Our Youtube Channel: Join Now
Feedback
Send your Feedback to feedback@javatpoint.com
Help Others, Please Share
Learn Latest Tutorials
Splunk tutorial SPSS tutorial Swagger T-SQL tutorial
tutorial
Splunk SPSS Transact-SQL
Swagger
Tumblr tutorial React tutorial Regex tutorial Reinforcement
learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning
R Programming RxJS tutorial React Native Python Design
tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns
https://www.javatpoint.com/javascript-deleting-cookies 10/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Python Pillow Python Turtle Keras tutorial
tutorial tutorial
Keras
Python Pillow Python Turtle
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions
Company
Interview
Questions
Company Questions
Trending Technologies
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Artificial Selenium Cloud Computing
Intelligence
Hadoop tutorial ReactJS Data Science Angular 7
Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps
https://www.javatpoint.com/javascript-deleting-cookies 11/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
B.Tech / MCA
DBMS tutorial Data Structures DAA tutorial Operating
tutorial System
DBMS DAA
Data Structures Operating System
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics
Ethical Hacking Computer Software html tutorial
Graphics Tutorial Engineering
Ethical Hacking Web Technology
Computer Graphics Software
Engineering
Cyber Security Automata C Language C++ tutorial
tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python tutorial List of
Framework Programs
Java Python
tutorial
Programs
.Net
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse
https://www.javatpoint.com/javascript-deleting-cookies 12/13
8/26/24, 9:36 AM JavaScript Deleting a Cookie - javatpoint
Leaf Refill Binder Notebook
5, Blank/Line/Grid/Cornel
ss
https://www.javatpoint.com/javascript-deleting-cookies 13/13