Setting cookies

Let's take a look at how to access and set cookies using JavaScript:

Information found within a cookie is in the key=value; format. Let us create some cookies in the browser using the code snippet below:

document.cookie = "myFirstCookie=good;"document.cookie = "mySecondCookie=great;"console.log(document.cookie);

Warning: Strange behavior ahead!

What do you expect to be logged here? The answer is as follows:

Why, and why wasn't the document.cookie object overwritten? All will be explained in the next section.

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.