{"id":8852,"date":"2025-04-26T16:50:58","date_gmt":"2025-04-26T11:20:58","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=8852"},"modified":"2025-04-26T16:54:15","modified_gmt":"2025-04-26T11:24:15","slug":"understanding-javascript-cookies","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/understanding-javascript-cookies\/","title":{"rendered":"JavaScript Cookies"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">JavaScript Cookies: Set, Get, and Delete<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Small data files called cookies are kept in the user&#8217;s browser and are frequently used to track sessions, remember user preferences, or store temporary data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What Are Cookies?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Cookies are key-value pairs that websites save in the browser. They enable websites to preserve stateful data (such as user login status) by being sent with each HTTP request to the server. Each cookie may have properties such as path, domain, and expiration date.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting Cookies with JavaScript<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You assign a string to a document in order to set a cookie. cookie with the following format: path=\/; expires=date; name=value. The path=\/ makes the cookie available throughout the website, and the expires attribute specifies when the cookie expires.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function setcookies(){\n  \/\/ console.log(\"hi\");\n  let date = new Date();\n  \/\/ console.log(\"hi2\"+date);\n  date.setTime(date.getTime()+(7*24*60*60*1000));\n  let expire = \"expire=\"+ date.toUTCString();\n  \/\/ console.log(\"hi2\"+expire);\n  document.cookie= \"username=Sujith;\"+ expire + \"; path=\/\"; \/\/ property\n  alert(\"Cookie set : username=Sujith\")\n}\n\n<img fetchpriority=\"high\" decoding=\"async\" width=\"2880\" height=\"922\" class=\"wp-image-8857\" style=\"width: 800px\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1.png\" alt=\"\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1.png 2880w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1-300x96.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1-1024x328.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1-768x246.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1-1536x492.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1-2048x656.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.50.43\u202fPM-1-850x272.png 850w\" sizes=\"(max-width: 2880px) 100vw, 2880px\" \/><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function creates a cookie with the specified name and value, and optionally sets an expiration date based on the number of days.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Getting Cookies with JavaScript<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The document must be parsed in order to retrieve a cookie. cookie string, which includes a list of all cookies separated by semicolons. By splitting this string, you can look up the desired cookie by name.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function getCookie(name) {\n  const nameEQ = name + \"=\";\n  const cookies = document.cookie.split(\";\");\n  for (let cookie of cookies) {\n    cookie = cookie.trim();\n    if (cookie.startsWith(nameEQ)) {\n      return cookie.substring(nameEQ.length);\n    }\n  }\n  return null;\n}\n\n\/\/ Get the value of the \"username\" cookie\nconst username = getCookie(\"username\");\nconsole.log(username);\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This function searches for a cookie by name and returns its value or null if not found.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"2876\" height=\"828\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM.png\" alt=\"\" class=\"wp-image-8862\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM.png 2876w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM-300x86.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM-1024x295.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM-768x221.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM-1536x442.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM-2048x590.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.46.45\u202fPM-850x245.png 850w\" sizes=\"(max-width: 2876px) 100vw, 2876px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Deleting Cookies with JavaScript<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To delete a cookie, you set its expiration date to a time in the past. This causes the browser to remove it immediately.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function deletecookies(){\n  \/\/ document.cookie = \"username = ;\" + expire + \"Thu, 01 jan 1999 00:00:00 UTC; path=\/\";\n  document.cookie = \"username=; expire = Thu, 01 jan 1970 00:00:00 UTC; path=\/\";\n  alert(\"cookie deleted\")\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By setting the expires attribute to a past date (e.g., 1970), the cookie is marked for deletion.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM.png\"><img decoding=\"async\" width=\"1024\" height=\"334\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-1024x334.png\" alt=\"\" class=\"wp-image-8863\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-1024x334.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-300x98.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-768x250.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-1536x500.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-2048x667.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-4.47.55\u202fPM-850x277.png 850w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Full Example: Code<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>    function setcookies(){\n      \/\/ console.log(\"hi\");\n      let date = new Date();\n      \/\/ console.log(\"hi2\"+date);\n      date.setTime(date.getTime()+(7*24*60*60*1000));\n      let expire = \"expire=\"+ date.toUTCString();\n      \/\/ console.log(\"hi2\"+expire);\n      document.cookie= \"username=Sujith;\"+ expire + \"; path=\/\"; \/\/ property\n      alert(\"Cookie set : username=Sujith\")\n    }\n\n    function getCookie(name) {\n      const nameEQ = name + \"=\";\n      const cookies = document.cookie.split(\";\");\n      for (let cookie of cookies) {\n        cookie = cookie.trim();\n        if (cookie.startsWith(nameEQ)) {\n          return cookie.substring(nameEQ.length);\n        }\n      }\n      return null;\n    }\n\n    \/\/ Get the value of the \"username\" cookie\n    const username = getCookie(\"username\");\n    console.log(username);\n\n    function deletecookies(){\n      \/\/ document.cookie = \"username = ;\" + expire + \"Thu, 01 jan 1999 00:00:00 UTC; path=\/\";\n      document.cookie = \"username=; expire = Thu, 01 jan 1970 00:00:00 UTC; path=\/\";\n      alert(\"cookie deleted\")\n    }\n  \n<img loading=\"lazy\" decoding=\"async\" width=\"2880\" height=\"998\" class=\"wp-image-8860\" style=\"width: 800px\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM.png\" alt=\"\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM.png 2880w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM-300x104.png 300w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM-1024x355.png 1024w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM-768x266.png 768w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM-1536x532.png 1536w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM-2048x710.png 2048w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2025\/04\/Screenshot-2025-04-26-at-3.58.46\u202fPM-850x295.png 850w\" sizes=\"(max-width: 2880px) 100vw, 2880px\" \/>\n\n\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A straightforward yet effective method of storing data in the browser is through JavaScript cookies. Effective user data management is possible with the help of the setCookie, getCookie, and deleteCookie functions. When using cookies, always keep security and browser compatibility in mind.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Cookies: Set, Get, and Delete Small data files called cookies are kept in the user&#8217;s browser and are frequently [&hellip;]<\/p>\n","protected":false},"author":514,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-8852","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-2iM","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/514"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=8852"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/8852\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=8852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=8852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=8852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}