Set a Cookie that never expires with JavaScript

Posted by I. B. Gd Pramana A. Putra, 11 Oct 22, last updated 11 Oct 22

You can not create a cookie that never expires (in theory), but you can set a cookie with the longest value possible for the expiry date value.

document.cookie = "my_cookie=cookie_value; expires=Fri, 31 Dec 9999 21:10:10 GMT";

Replace my_cookie and cookie_value with any cookie value that you desire. Note that 31 Dec 9999 is the highest value that we can set for the cookie to be never expired (practically).

Reference: MSDN - Browser Cookie

I love sharing code snippets as most of the time, a quick code example is what we're looking for instead of long-written articles. If you think my code snippets are helpful and save you a lot of time, please consider buying me a cup of coffee :)

Support me via · paypal · buymeacoffee · ko-fi · trakteer
Contributed Snippets
Answer & Responses
    No comments yet

Wanna write a response?

You have to login before write a comment to this post.