List all cookies with JavaScript

How can I list all cookies with JavaScript?

Last updated:7/19/2021 5:48:18 AM

1 Answers

Ethan Karla
Ethan Karla

Read a Cookie with JavaScript

With JavaScript, cookies can be read like this:

let x = document.cookie;

document.cookie will return all cookies in one string much like: cookie1=value; cookie2=value; cookie3=value;

You can list cookies for current domain:

function listCookies() {

    var theCookies = document.cookie.split(';');
    var aString = '';
    for (var i = 1 ; i <= theCookies.length; i++) {
        aString += i + ' ' + theCookies[i-1] + '\n';
    }
    return aString;
}

But you cannot list cookies for other domains for security reasons

Hope this information has cleared your confusion.

Happy Coding!

Answer