Cookies versus local storage
By now, you may have observed that cookies and local storage serve almost completely different purposes. The only thing they have in common is that they store data. The following is a brief comparison of cookies and local storage:
Cookies | Local storage |
Cookies are transferred to a server on every request automatically by the browser | To transfer local storage data to a server, you need to manually send an Ajax request or send it through hidden form fields |
If data needs to be accessed and read both by the client and server, use cookies | If the data needs to be accessed and read only by the client, use local storage |
Cookies can have an expiration date, after which they are automatically deleted | Local storage provides no such expiration date feature; it can only be cleared by JavaScript |
The maximum size of a cookie is 4 KB | The maximum size of local storage depends on the browser and platform, but it is usually around 5 MB per domain |