How can I set a cookie to a variable with javascript?

  cookies, javascript

How can I set a JavaScript cookie to an already-defined variable WITHOUT using PHP?
My current code is this:

document.cookie = "cookieName=varName";

but that obviously does not work.
Thank you in advance!

I can’t find anything else on any sites where I’ve looked, like https://developer.mozilla.org and https://w3schools.com, and also on stack overflow. There were many things about setting a pre-defined variable to a pre-defined variable, though.

EDIT: To clarify, I did find one duplicate of this question but could not really understand the answers. Please do not mark this as a duplicate.

Source: Ask Javascript Questions

LEAVE A COMMENT