How to get the Cookies storage

Hi, can I ask for some help please how can I get the Cookies storage ? in localstorage we can do this

localStorage.getItem('token');

but in Cookie I have no idea how.

Thank you in advance.

Just use

Let cookies = document.cookie;
1 Like

JavaScript doesn’t have any native ways to manage cookies, so here’s some cookie handling functions

function createCookie(name, value, days) {
    var expires = "";
    var date = new Date();
    if (days) {
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var allCookies = document.cookie.split(";");
    allCookies.forEach(function (cookie) {
        while (cookie.charAt(0) === " ") {
            cookie = cookie.substring(1, cookie.length);
        }
        if (cookie.indexOf(nameEQ) === 0) {
            return cookie.substring(nameEQ.length, cookie.length);
        }
    });
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}
1 Like

Thank you so much Paul_Wilkins :slight_smile:

Easiest way to go:

console.log(document.cookie)