How to parse HTTP Cookie header and return an object of all cookie name-value pairs in JavaScript ? Last Updated : 06 Mar, 2024 Comments Improve Suggest changes Like Article Like Report To parse HTTP Cookie header we will spilt the cookies data and create objects from the key-value extracted from cookies. Cookies are simply small text files that a web server sends to the user's browser. They contain the following data. Name-value pair with actual data.The expiry date for when the cookie becomes invalid.Domain and path of the server it should be sent to.ApproachTo retrieve all the stored cookies in JavaScript, we can use the document.cookie property but this property returns a single string in which the key-value pair is separated by a ";". It would be great if we can store the key-value pair into an object as it would make the retrieval process much easier. JavaScript does not provide any methods for such a scenario. So let's work around this problem. We need to create a function that will parse the cookie string and would return an object containing all the cookies. This would be a simple process with the following steps. Get each individual key-value pair from the cookie string using string.split(";").Separate keys from values in each pair using string.split("=").Create an object with all key-value pairs and return the object.Example: This code defines a cookieParser function that takes a cookie string as input and returns an object containing the parsed key-value pairs. The provided example usage with dummyCookieString demonstrates how the function works.The console.log statements at the end print the values of specific cookies ('gfg' and 'foo') from the parsed object. JavaScript function cookieParser(cookieString) { // Return an empty object if cookieString // is empty if (cookieString === "") return {}; // Get each individual key-value pairs // from the cookie string // This returns a new array let pairs = cookieString.split(";"); // Separate keys from values in each pair string // Returns a new array which looks like // [[key1,value1], [key2,value2], ...] let splittedPairs = pairs.map(cookie => cookie.split("=")); // Create an object with all key-value pairs const cookieObj = splittedPairs.reduce(function (obj, cookie) { // cookie[0] is the key of cookie // cookie[1] is the value of the cookie // decodeURIComponent() decodes the cookie // string, to handle cookies with special // characters, e.g. '$'. // string.trim() trims the blank spaces // auround the key and value. obj[decodeURIComponent(cookie[0].trim())] = decodeURIComponent(cookie[1].trim()); return obj; }, {}) return cookieObj; } let dummyCookieString = "username=John; gfg=GeeksForGeeks; foo=education"; // Pass document.cookie to retrieve actual cookies let cookieObj = cookieParser(dummyCookieString); console.log(`cookie gfg has value ${cookieObj['gfg']}.`); console.log(`cookie foo has value ${cookieObj['foo']}.`); Outputcookie gfg has value GeeksForGeeks. cookie foo has value education. Comment More infoAdvertise with us Next Article How to parse HTTP Cookie header and return an object of all cookie name-value pairs in JavaScript ? P pansaripulkit13 Follow Improve Article Tags : JavaScript Web Technologies javascript-functions HTTP JavaScript-Questions +1 More Similar Reads How to serialize a cookie name-value pair into a Set-Cookie header string in JavaScript ? In this article, you will understand to serialize a cookie with name and value into a Set-Cookie header string. Basically, here we are trying to pass the cookie with a compatible set-cookie string format. Cookie: Cookies are small blocks of data created by a web server while a user is browsing a web 2 min read How to get an object containing parameters of current URL in JavaScript ? The purpose of this article is to get an object which contains the parameter of the current URL. Example: Input: www.geeksforgeeks.org/search?name=john&age=27 Output: { name: "john", age: 27 } Input: geeksforgeeks.org Output: {} To achieve this, we follow the following steps. Create an empty obj 2 min read How to Find Property Values in an Array of Object using if/else Condition in JavaScript ? Finding property values in an array of objects using if/else condition is particularly useful when there is a collection of objects. Table of ContentUsing Array Find MethodUsing Array Filter MethodUsing For Of LoopUsing Array Map MethodUsing Array Reduce MethodUsing Array Some MethodUsing Array Find 5 min read How to get an array of function property names from own enumerable properties of an object in JavaScript ? Enumerable Properties: All the properties of an object that can be iterated using for..in loop or Object.keys() method are known as enumerable properties. In this article, we will see how to get an array of functions of an object that are enumerable. It can be achieved by following these 2 steps. Us 2 min read How to set up a cookie that never expires in JavaScript ? We can set up a cookie that never expires in JavaScript using the following approach:Prerequisites :Intermediate level knowledge of JavaScriptBasic HTML Disclaimer: All the cookies expire as per the cookie specification. So, there is no block of code you can write in JavaScript to set up a cookie th 2 min read How to Convert String of Objects to Array in JavaScript ? This article will show you how to convert a string of objects to an array in JavaScript. You have a string representing objects, and you need to convert it into an actual array of objects for further processing. This is a common scenario when dealing with JSON data received from a server or stored i 3 min read How to read properties of an Object in JavaScript ? Objects in JavaScript, it is the most important data type and forms the building blocks for modern JavaScript. These objects are quite different from JavaScriptâs primitive data-types(Number, String, Boolean, null, undefined, and symbol) in the sense that these primitive data-types all store a singl 2 min read How to Set, View and Manipulate Cookies using 'Response.cookie()' and Postman ? Cookies enable websites to store small pieces of information on a user's device. It helps enhance user experience and enable various functionalities. In this article, we'll explore a simple way to manipulate cookies using the 'Response.cookie()' functionPrerequisite:Basics of NodejsBasics of Express 2 min read How to print the content of an object in JavaScript ? To print the content of an object in JavaScript we will use JavaScript methods like stringify, object.values and loops to display the required data. Let's first create a JavaScript Object containing some key-values as given below: JavaScript // Given Object const obj = { name: 'John', age: 30, city: 3 min read How to list all the cookies of the current page using JavaScript ? Cookies are small pieces of data stored by websites on your browser to remember information about your visit, such as login status, preferences, or other settings. In JavaScript, you can easily retrieve cookies stored by the current domain using document.cookie. However, due to security reasons, you 3 min read Like