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.
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: Refer to the comments in the following code for better understanding.
cookie gfg has value GeeksForGeeks. cookie foo has value education.