JavaScript: how to remove and clear a cookie

JavaScript: how to remove and clear a cookie

In this article we will see how to clear a cookie in JavaScript.

In this article we will see how to clear a cookie in JavaScript.

Knowing the name of the cookie, we can write a function that sets its expiration to the past in order to force the browser to no longer consider it valid.

const removeCookie = name => {
    document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/`;
};