How to set JavaScript Cookie with no expiration date?

In this tutorial, we will learn how to set JavaScript cookies with no expiration date. Cookies are small data files stored by web browsers that help websites remember user preferences and track behavior. When a cookie has no expiration date, it becomes a session cookie that lasts until the browser is closed.

Understanding Cookie Expiration

The expires attribute in JavaScript cookies is optional. If you don't specify an expiration date, the cookie will automatically expire when the browser session ends (when the user closes the browser). This type of cookie is called a session cookie.

Syntax for Setting Session Cookies

To create a cookie without an expiration date, simply omit the expires attribute:

document.cookie = "cookieName=cookieValue; path=/";

Example: Creating and Reading Session Cookies

Here's a complete example that creates session cookies and displays them:

<html>
<head>
   <script>
      function setCookie() {
         // Set cookies without expiration date (session cookies)
         document.cookie = "username=john_doe; path=/";
         document.cookie = "theme=dark; path=/";
         document.cookie = "language=english; path=/";
         alert("Session cookies have been set!");
      }
      
      function ReadCookie() {
         var allcookies = document.cookie;
         document.write("All Cookies: " + allcookies + "<br><br>");
         
         // Get all the cookies pairs in an array
         var cookiearray = allcookies.split(';');

         // Parse each cookie and display key-value pairs
         for(var i = 0; i < cookiearray.length; i++) {
            var cookiePair = cookiearray[i].trim();
            var name = cookiePair.split('=')[0];
            var value = cookiePair.split('=')[1];
            document.write("Key: " + name + " | Value: " + value + "<br>");
         }
      }
   </script>
</head>
<body>
   <h3>JavaScript Session Cookies Demo</h3>
   <form name="cookieForm">
      <p>Click the buttons to set and read session cookies:</p>
      <input type="button" value="Set Cookies" onclick="setCookie()" />
      <input type="button" value="Read Cookies" onclick="ReadCookie()" />
   </form>
</body>
</html>

Session Cookies vs Persistent Cookies

Cookie Type Expiration Lifetime Use Case
Session Cookie No expires attribute Until browser closes Temporary data, login sessions
Persistent Cookie Has expires/max-age Until specified date User preferences, long-term storage

Creating Different Types of Cookies

<script>
   // Session cookie (no expiration)
   document.cookie = "sessionData=temporaryValue; path=/";
   
   // Persistent cookie (expires in 7 days)
   var expireDate = new Date();
   expireDate.setDate(expireDate.getDate() + 7);
   document.cookie = "persistentData=permanentValue; expires=" + expireDate.toUTCString() + "; path=/";
   
   console.log("Cookies set successfully!");
   console.log("All cookies: " + document.cookie);
</script>

Key Points About Session Cookies

  • Session cookies are automatically deleted when the browser is closed

  • They don't require an expires or max-age attribute

  • Perfect for storing temporary data like shopping cart contents or login sessions

  • More secure than persistent cookies as they have limited lifetime

Conclusion

Session cookies provide a simple way to store temporary data that automatically expires when the browser closes. By omitting the expires attribute, you create cookies that last only for the current browser session, making them ideal for temporary user data and enhanced security.

Updated on: 2026-03-15T22:24:44+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements