How to set and get the simplest cookie ever with a JavaScript command?

Primarily for the sake of learning and testing I want to set the simplest cookie ever, containing only one letter and then get it.


In page A I run in console:

set cookie myCookie = "a"

In page B I run in console:

get myCookie

I assume that “get” here may be a console.log(myCookie) or a document.write(myCookie).

My problem

I am not sure I understand how is it done. For example:

document.cookie = "username=John Doe";

I don’t find any naming being done.
I mean, if we set a variable we give it a name such as in:

const bestNameEverInHistory = "a"`

Then we can console.log(bestNameEverInHistory); in document.cookie.

How could we console.log() a cookie if there is no naming?

1 Like
1 Like

So it’s like an HTML command with attribute/s and value/s output.

Okay. The syntax there is a bit confusing to me.

1 Like


What I don’t get is why there is a formal way to display one or more cookie/s output:

allCookies = document.cookie;

Instead of just letting users to pick and choose common ways to display output in JavaScript such as these?


I even got different colors in console:


1 Like

document.cookie is a string. You can do anything with that string you can with any other string in javascript.


I thought that there are two or more cookie strings.

I guess than, that cookies is not what I need for this task, rather, local storage is what I need for it.

depends on what you’re trying to store in the cookie.
The thing you pasted a screenshot of above is… at least 6 cookies. one called _gc1_au, one called __qca, one called _hjSessionUser_1088957, one called _gid, one called _ga_QPSK9L63NG, and one called _ga.

Cookies are for sending information to the server (or more specifically, storing information the server sends to you, so you can send it back if needed). If you’re trying to store information locally that originates from the user, localStorage would be the better option.

1 Like

Alright !

I do wonder, why are the names of the cookies above so “technical”?
Shouldn’t there be more “human readable” names?

Why? They’re not for humans to read.
When’s the last time you read your cookies? Have you read your cookies from this site?

Today was the first and so far last.

But I have read maybe 10,000 dramatic “cookie statements” in various websites in the last 5 years or so…

That made me think, that if people store cookies in the user’s browser, they should make them clear if that user wants to read their content… Maybe as a legal requirement…

It’s machine code for machine use. I can write my javascript to say

var thisIsAThingToDoSomeStuff = 4

or i can say

var x = 4

If you never read it, what’s the difference? Your browser doesnt care what it’s called, it just knows there is a thing, and it has a value of 4.

Most of the cookies in your string, for the record, are parts of Google Analytics tracking cookies that basically every major site in the world uses. Do you care? Does it make it any different now that you know what they’re for?

That’s a good question :slight_smile:

I think most of the time I don’t care. But I can’t say that I’ll never care :joy:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.