Local Storage vs Cookies (The storage showdown)

18 Jun, 2019

Local site storage can open up a ton of possibilities in web design and breath new life into an otherwise static webpage. Whether you just want to do something simple like “show a welcome message only once”, or something a little more complex like “store an array of user preferences”. Without some form of local site storage, this would not be possible.

When it comes to local site storage, there are two main options you have to choose from, Cookies and Local Storage. Each are best suited to a slightly different use case.

A handy tip before we get started. In Chrome, click F12 to open the developer tools and click on “Application”. Here you'll be able to see all the local storage, cookies and session data your site is saving.

Local Storage
Local storage gives you 10MB of space to work with. This makes local storage ideal for most situations where you need to store lots of data locally. Managing local storage is really easy to use compared to cookies as javascript provides a "localStorage" class that handles everything for you. However, HTML5 will need to be supported by the user's browser for local storage to work.

//Storing data
localStorage.setItem("favoriteColor", "red");

//Retrieving stored data
var someData = localStorage.favoriteColor;

//Deleting stored data

//Clear localStorage

Unlike cookies, local storage doesn't have an expiration date. It won't be removed from the user's computer unless the user, either clears their browser storage, or the data is deleted by the site itself.

The main reason you may want to use cookies over local storage is that cookies are sent to, and can be accessed by the server when making requests. This is especially useful if you need to personalize the server's response based on stored cookie data.

The main downside of cookies is that they only allow for 4KB of usable space so if you were planning on storing anything larger than that then local storage might be your best bet. Because of this, cookies are usually only used to store small bits of information like session IDs to keep the user logged in.

When creating a cookie in javascript, you can specify when you want it to expire, by default its set to expire when the window closes. You can also specify what page the cookie belongs to, this will be useful if you ever want to delete the cookie.

//Creating a new cookie
document.cookie = "favoriteColor=red";

//Additional parameters
document.cookie = "favoriteColor=red; expires=Mon, 21 Dec 2020 12:00:00 UTC; path=/";

To delete a cookie simply override the same cookie with an expiry date that has already passed, make sure you specify the same path:

document.cookie = "favoriteColor=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";

Retrieving the actual data from the cookies is a bit more complicated than using the built-in javascript localStorage class. The cookies are stored as a string so when you retrieve the cookie using:

var someData = document.cookie;

You will then need to parse the string yourself to get the individual cookie values. If you're looking for some premade functions to handle this, or just want to learn more, I would recommend checking out the article w3schools did on cookies here.

Other Tutorials

View All

Beme style progress bar

html - css - javascript

How to create a progress bar that wraps around the screen of any sized device similar to the Beme app.

Building a custom chromecast web app

html - javascript

Chromecasts make streaming your favourite songs, games and movies easy. But if you're like me, you might be wondering what's going on behind the scenes when you cast an app, and how you can build your own.

Using variables in CSS

css - javascript

When styling your web app, you've probably found yourself copying hex colour codes, margin parameters or other styles to several elements to achieve a consistent design across your app.