JavaScript Deleting Cookies

Javascript facilitates multiple ways of deleting cookies. These are as follows:

1. Delete the cookie by using the expire attribute

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
document.cookie = 'name=Mahesh; expires=Sun, 19 May 2019 18:04:55 UTC'
document.cookie = 'name=Mahesh; expires=Sun, 19 May 2019 18:04:55 UTC'
document.cookie = 'name=Mahesh; expires=Sun, 19 May 2019 18:04:55 UTC'

2. Delete the cookie by using the max-age attribute

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
document.cookie = 'name=Vishal; max-age=7200'
document.cookie = 'name=Vishal; max-age=7200'
document.cookie = 'name=Vishal; max-age=7200'

3. Delete cookie explicitly by using a web browser
Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="set" onclick="setCookie()">
<input type="button" value="get" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username = COOKIE;expires=Sun, 28 Nov 2018 13:15:00 UTC";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("No Cookie here");
}
}
</script>
</body>
</html>
<!DOCTYPE html> <html> <head> </head> <body> <input type="button" value="set" onclick="setCookie()"> <input type="button" value="get" onclick="getCookie()"> <script> function setCookie() { document.cookie="username = COOKIE;expires=Sun, 28 Nov 2018 13:15:00 UTC"; } function getCookie() { if(document.cookie.length!=0) { alert(document.cookie); } else { alert("No Cookie here"); } } </script> </body> </html>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" value="set" onclick="setCookie()">
<input type="button" value="get" onclick="getCookie()">
<script>
function setCookie()
{
document.cookie="username = COOKIE;expires=Sun, 28 Nov 2018 13:15:00 UTC";
}
function getCookie()
{
if(document.cookie.length!=0)
{
alert(document.cookie);
}
else
{
alert("No Cookie here");
}
}
</script>
</body>
</html>