How do I create and read a value from cookie in JavaScript?
Read a Value from cookie in JavaScript
460
19-Jul-2021
Updated on 29-Nov-2023
Aryan Kumar
29-Nov-2023To read a value from a cookie in JavaScript, you can use the document.cookie property. The document.cookie property contains all the cookies associated with the current document. Here's an example of how you can read a value from a cookie:
In this example, the getCookie function takes a cookie name as a parameter, splits the document.cookie string into individual cookies, and then searches for the cookie with the specified name. If found, it returns the value of that cookie. If not found, it returns null.
Remember that cookies are stored as key-value pairs separated by semicolons. The startsWith method is used to check if a particular cookie starts with the specified name.