'Set sessionStorage expiration

I have this code which sets a sessionStorage. It works fine but I would like to set an expiration date of 24 hours. Can I do this? I know this can be done with cookies but not sure with sessionStorage objets.

<script>
  
  var pageViewed = parseInt("1");
  
  if(sessionStorage.pageViewed)
  {
    var intSessionStoragePageViewed = parseInt(sessionStorage.pageViewed);
    pageViewed =  intSessionStoragePageViewed + 1;
    sessionStorage.setItem("pageViewed", pageViewed);
  }
  else if(!sessionStorage.pageViewed)
  {
    
    sessionStorage.setItem("pageViewed", pageViewed);
  }
  
  </script>


Solution 1:[1]

localStorage and sessionStorage do not have built-in expiration dates like cookies do. sessionStorage is erased at the end of the browser session; localStorage is erased only when you or the end user explicitly erases it.

If you want locally stored data to expire after a specific duration, use cookies.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Beck