37

If I get Chrome to show me document.cookie by going into the console and typing document.cookie; it'll give me, say:

"name=John; gender=male";

But then if I type in, say, document.cookie = 5; all it does is add 5; to the start of the string, so I get:

"5; name=John; gender=male";

If I try document.cookie = null; then it doesn't even do anything.

How can this be? It's a variable, isn't it? So why isn't the assignment operator working the way it should? Is it actually just a bit of syntactic sugar rather than a real variable? And if so, what precisely is the sugar covering up?

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Jack M
  • 4,769
  • 6
  • 43
  • 67
  • 1
    As an aside, I don't know the answer to this question, but many languages allow operators (including assignment in some, oddly enough) to be overwritten with custom behaviors. This is very useful, but can lead to confusion too, such as what you are having here. – Matthew Scharley Jul 22 '11 at 15:09
  • 1
    @Matthew: JavaScript isn't one of them, except that host objects (like `document`) can do nearly anything they want, because they're not native objects. – T.J. Crowder Jul 22 '11 at 15:36

3 Answers3

39

document.cookie has very special behavior. As you've seen, assigning to it adds (or updates) a cookie (or multiple cookies), rather than replacing all of the cookies. It's very unusual.

Read all about it on MDN.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 2
    @Daniel: No disagreement here. I can't imagine why it would need to be so...obtuse, even back in the day. I mean, an array-like thing with `Cookie` elements, how cutting edge! ;-) – T.J. Crowder Jul 22 '11 at 15:15
  • 1
    So how is this "unusual" behavior implemented? Is it at the interpreter level or does javascript support some kind of operator redefining? – Jack M Jul 22 '11 at 15:29
  • 7
    @Jack: `document` is what the ECMAScript specification calls a "host object," not a native object. Host objects can do nearly anything they want to, their semantics lie mostly outside the specification. Ultimately, [the assignment](http://es5.github.com/#x11.13.1) becomes an abstract *[PutValue](http://es5.github.com/#x8.7.2)* call on the host object. What the host environment (browser, in this case) does with that is up to the host environment. – T.J. Crowder Jul 22 '11 at 15:35
  • 2
    I wish i could upvote more...This info saved my day. – Giannis Paraskevopoulos Jun 23 '14 at 09:59
7

Why not have a look at MDN?

The string on the right side of the assignment operator to document.cookies should be a semicolon separated list of key-value pairs, i.e. document.cookie = "aKey=5" will set/update the aKey cookie.

So yes, document.cookie shows special behavior.

ascripter
  • 5,665
  • 12
  • 45
  • 68
Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • 2
    I hope I don't come across as nit-picking here, but was the pluralisation of your first `document.cookie` intentional? I'm not sure if this is a distinct concept. – scubbo Oct 11 '12 at 14:29
  • Also, it now says "This article is in need of a technical review." It is still a great reference page though. Thank you! – Ellie Kesselman Feb 08 '14 at 18:32
3

Here is an example of your "issue". Also, it says the following:

You can delete a cookie by simply updating its expiration time to zero.

Sascha Galley
  • 15,711
  • 5
  • 37
  • 51