{"id":2400,"date":"2026-11-23T08:41:40","date_gmt":"2026-11-23T08:41:40","guid":{"rendered":"https:\/\/www.woodcentral.com\/-\/peter\/?p=2400"},"modified":"2026-11-23T08:41:41","modified_gmt":"2026-11-23T08:41:41","slug":"cookies-vs-localstorage","status":"publish","type":"post","link":"https:\/\/www.woodcentral.com\/-\/peter\/cookies-vs-localstorage\/","title":{"rendered":"Cookies vs. LocalStorage"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Cookies and localstorage<\/strong> are both ways for websites to store small amounts of data in your browser, but they serve very different purposes and behave quite differently.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s a clear comparison:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Feature<\/th><th>Cookies<\/th><th>LocalStorage<\/th><\/tr><\/thead><tbody><tr><td><strong>Purpose<\/strong><\/td><td>Mainly for server-client communication (sessions, tracking, preferences)<\/td><td>Client-side only data storage (user preferences, saved projects, settings)<\/td><\/tr><tr><td><strong>Sent with requests<\/strong><\/td><td>Yes \u2014 automatically sent with every HTTP request to the same domain<\/td><td>No \u2014 never sent to the server<\/td><\/tr><tr><td><strong>Storage limit<\/strong><\/td><td>About 4 KB per cookie<\/td><td>About 5\u201310 MB per origin (much larger)<\/td><\/tr><tr><td><strong>Expiration<\/strong><\/td><td>Can expire (session cookies or set expiration date)<\/td><td>Permanent until manually deleted<\/td><\/tr><tr><td><strong>Accessibility<\/strong><\/td><td>Can be set to HttpOnly (inaccessible to JavaScript)<\/td><td>Always accessible via JavaScript<\/td><\/tr><tr><td><strong>Security<\/strong><\/td><td>Can be more secure with HttpOnly and Secure flags<\/td><td>More exposed to XSS attacks<\/td><\/tr><tr><td><strong>Data type<\/strong><\/td><td>Only strings<\/td><td>Strings (but you can JSON.stringify objects)<\/td><\/tr><tr><td><strong>Use case example<\/strong><\/td><td>Login session, CSRF tokens, analytics tracking<\/td><td>Saving woodworking project dimensions, user theme preference, or a list of favorite tools<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Quick woodworking-related examples<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Cookie<\/strong>: A site might use a cookie to remember you\u2019re logged into the WoodCentral forum so you don\u2019t have to log in every time you visit.<\/li>\n\n\n\n<li><strong>LocalStorage<\/strong>: The site could save your preferred board-foot calculator settings or the last few lumber dimensions you entered so they\u2019re still there when you come back next week.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Rule of thumb<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong>cookies<\/strong> when the server needs to know the information.<\/li>\n\n\n\n<li>Use <strong>localStorage<\/strong> when only the browser needs to remember something and you want more storage space.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Cookies and localstorage are both ways for websites to store small amounts of data in your browser, but they serve very different purposes and behave quite differently. Here\u2019s a clear comparison: Feature Cookies LocalStorage Purpose Mainly for server-client communication (sessions, tracking, preferences) Client-side only data storage (user preferences, saved projects, settings) Sent with requests Yes &#8230; <a title=\"Cookies vs. LocalStorage\" class=\"read-more\" href=\"https:\/\/www.woodcentral.com\/-\/peter\/cookies-vs-localstorage\/\" aria-label=\"Read more about Cookies vs. LocalStorage\">Read more<\/a><\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-2400","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/posts\/2400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/comments?post=2400"}],"version-history":[{"count":1,"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/posts\/2400\/revisions"}],"predecessor-version":[{"id":2401,"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/posts\/2400\/revisions\/2401"}],"wp:attachment":[{"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/media?parent=2400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/categories?post=2400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.woodcentral.com\/-\/peter\/wp-json\/wp\/v2\/tags?post=2400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}