DEV Community

Kat
Kat

Posted on

Cookies

We use cookies every day but I've never really understood what they do or how they work. The lesson "the cookies hash" was helpful in elucidating the basic concept of what cookies are and what they do.

Essentially, cookies are "a set of key-value pairs" that the server stores in the client. Simple! Then, the data are sent back to the server with a header "Cookie" every time the client makes another HTTP request. Thus, the server can keep track of things like client preferences or the identity of a logged-in user.

The cookies hash can be accessed using the same hash methods as a regular hash. However, the keys and values can only be stored as strings. Thus, any other data type must be encoded as a string and later decoded back into the data type it was originally. The best way to do this is through the use of JSON strings. For this process, we use the methods JSON.parse(string) and JSON.generate(hash).

Top comments (0)