Book Image

Learn ECMAScript - Second Edition

By : MEHUL MOHAN, Narayan Prusty
Book Image

Learn ECMAScript - Second Edition

By: MEHUL MOHAN, Narayan Prusty

Overview of this book

Learn ECMAScript explores implementation of the latest ECMAScript features to add to your developer toolbox, helping you to progress to an advanced level. Learn to add 1 to a variable andsafely access shared memory data within multiple threads to avoid race conditions. You’ll start the book by building on your existing knowledge of JavaScript, covering performing arithmetic operations, using arrow functions and dealing with closures. Next, you will grasp the most commonly used ECMAScript skills such as reflection, proxies, and classes. Furthermore, you’ll learn modularizing the JS code base, implementing JS on the web and how the modern HTML5 + JS APIs provide power to developers on the web. Finally, you will learn the deeper parts of the language, which include making JavaScript multithreaded with dedicated and shared web workers, memory management, shared memory, and atomics. It doesn’t end here; this book is 100% compatible with ES.Next. By the end of this book, you'll have fully mastered all the features of ECMAScript!
Table of Contents (18 chapters)
Title Page
PacktPub.com
Contributors
Preface
Index

Storing data with cookies


Cookies are little strings, which, once set for a domain and path, are sent over and over to the server for every request. This is perfect for authentication, but not so good if you're using them to store some data that you need only once or that you need to access only on the frontend, such as a player's score in a game whose results you are not storing on the server.

People usually use cookies to store heavy data to make it available on some other path on the domain. This is a bad practice because you're transferring that data to the server all the time, and, if that data is heavy, it'll make your communication slow.

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...