Learn

As you can see, the localStorage syntax is very straightforward and makes storage and retrieval easy. So why hasn’t everything transitioned to using localStorage or sessionStorage? What are the pros and cons of each? Below, we’ll compare cookies and localStorage.

Though both cookies and browser storage are a form of client-side storage, they differ in a number of ways.

localStorage and sessionStorage are meant to be accessed directly by JavaScript and not sent directly to the server like cookies. localStorage is also newer, so there is less browser compatibility. The syntax to access this storage is also arguably simpler.

There is also the difference in data expiry and capacity. Cookie expiration can be configurated through code, but localStorage and sessionStorage expiration work differently and are more so dependent on browser operations. Cookies hold 4KB per domain, whereas localStorage and sessionStorage have greater capacity per domain.

Take a look at the side-by-side comparison!

Take this course for free

By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.
Already have an account?