How to get all cookies using JavaScript
In this article, we will show you how to get a list of all cookies using JavaScript. In the previous article, we explained how to set and get cookies in JavaScript.
Checkout more articles on JavaScript
Use the following function to get a list of all cookies of the browser using JavaScript.
1 2 3 4 5 6 7 8 9 | const getAllCookies = () => { const pairs = document.cookie.split(";"); let cookies = {}; for (let i=0; i<pairs.length; i++){ const pair = pairs[i].split("="); cookies[(pair[0]+'').trim()] = unescape(pair.slice(1).join('=')); } return cookies; } |
This function will give you the result in the form of an object.
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂