How to update and delete a cookie?

The cookie API is kind of lame. Let me clarify…

You don’t update cookies; you overwrite them:

    document.cookie = "username=Arnold"; // Create 'username' cookie
    document.cookie = "username=Chuck"; // Update, i.e. overwrite, the 'username' cookie to "Chuck"

You also don’t delete cookies; you expire them by setting the expires key to a time in the past (-1 works too).

Source: https://developer.mozilla.org/en-US/docs/Web/API/document.cookie

Leave a Comment