{"id":7946,"date":"2021-08-14T12:39:32","date_gmt":"2021-08-14T12:39:32","guid":{"rendered":"https:\/\/www.scmgalaxy.com\/tutorials\/?p=7946"},"modified":"2022-04-13T16:30:44","modified_gmt":"2022-04-13T16:30:44","slug":"how-many-create-cookies-in-javascript","status":"publish","type":"post","link":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/","title":{"rendered":"How many create cookies in JavaScript?"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><strong>JavaScript Cookies<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Cookies are exposed as the cookie property of the Document object. This property is both readable and writeable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can see Cookies in Google Chrome by following chrome:\/\/settings\/content\/cookies<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Creating Cookies<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">When you assign a string to document.cookie, the browser parses it as a cookie and adds it to its list of cookies. There are several parts to each cookies, many of them optional.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Syntax: &#8211;<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\" id=\"block-0bbc4906-32da-4254-a4a7-5076d7e5c92b\">document.cookie = \u201cname=value\u201d;<br>document.cookie = \u201cname=value; expires=date; domain=domain; path=path; secure\u201d;<br>document.cookie = \u201cname=value; max-age=inSecond; domain=domain; path=path; secure\u201d;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Ex:-<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/67579ff638772d712da0e0b9f9596bc1.js\"><\/script>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Creating Cookies<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Optional Cookies Attribute:-<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">max-age<br>expires<br>domain<br>path<br>secure<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Whenever you omit the optional cookie fields, the browser fills them in automatically with reasonable defaults.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>max-age<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">It is used to create persistent cookies. It is supported by all modern browsers except IE.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Type of cookies: &#8211;<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Session Cookies \u2013 Cookies that are set without the expires\/max-age field are called session cookies. It is destroyed when the user quits the browser.<\/li><li>Persistent Cookies \u2013 The browser keeps it up until their expiration date is reached.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Ex:-<\/h2>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/f1303dff9cb90e8d60788a9db973d9d8.js\"><\/script>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>expires<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">It is used to create persistent cookies.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Type of cookies: &#8211;<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>Session Cookies \u2013 Cookies that are set without the expires\/max-age field are called session cookies. It is destroyed when the user quits the browser.<\/li><li>Persistent Cookies \u2013 The browser keeps it up until their expiration date is reached.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">E<strong>x:-<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">document.cookie = \u201cusername=devops; expires=Monday, 3-Sep-2018 09:00:00 UTC\u201d;<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>domain<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">It specifies the domain for which the cookie is valid. If not specified, this defaults to the host portion of the current document location. If a domain is specified, subdomains are always included.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ex: &#8211;<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/vijaypk376\/cdde12250b6aee1fcd43e34322d6ed6f.js\"><\/script>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>path<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Path can be \/ (root) or \/mydir (directory). If not specified, defaults to the current path of the current document location, as well as its descendants.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ex: &#8211; document.cookie = \u201cusername=devops; path=\/\u201d;<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ex: &#8211; document.cookie = \u201cusername=devops; path=\/home\u201d;<\/strong><\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>secure<\/strong><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Cookie to only be transmitted over secure protocol as https. Before Chrome 52, this flag could appear with cookies from http domains.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Ex: &#8211; document.cookie = \u201cusername=devops; secure\u201d;<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>JavaScript Cookies Cookies are exposed as the cookie property of the Document object. This property is both readable and writeable. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[16],"tags":[5251,5161,5999,4696,5095,5310],"class_list":["post-7946","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-chrome","tag-cookies","tag-document-object","tag-google","tag-javascript","tag-property"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How many create cookies in JavaScript? - scmGalaxy<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How many create cookies in JavaScript? - scmGalaxy\" \/>\n<meta property=\"og:description\" content=\"JavaScript Cookies Cookies are exposed as the cookie property of the Document object. This property is both readable and writeable. [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"scmGalaxy\" \/>\n<meta property=\"article:published_time\" content=\"2021-08-14T12:39:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-04-13T16:30:44+00:00\" \/>\n<meta name=\"author\" content=\"rajeshkumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rajeshkumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/\"},\"author\":{\"name\":\"rajeshkumar\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"headline\":\"How many create cookies in JavaScript?\",\"datePublished\":\"2021-08-14T12:39:32+00:00\",\"dateModified\":\"2022-04-13T16:30:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/\"},\"wordCount\":353,\"commentCount\":0,\"keywords\":[\"chrome\",\"Cookies\",\"document object\",\"Google\",\"Javascript\",\"property\"],\"articleSection\":[\"JavaScript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/\",\"name\":\"How many create cookies in JavaScript? - scmGalaxy\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\"},\"datePublished\":\"2021-08-14T12:39:32+00:00\",\"dateModified\":\"2022-04-13T16:30:44+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/how-many-create-cookies-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How many create cookies in JavaScript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#website\",\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/\",\"name\":\"scmGalaxy\",\"description\":\"A Platform for Software Engineers\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/#\\\/schema\\\/person\\\/a40e0dd38feee7c2779460a804d49361\",\"name\":\"rajeshkumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g\",\"caption\":\"rajeshkumar\"},\"url\":\"https:\\\/\\\/www.scmgalaxy.com\\\/tutorials\\\/author\\\/rajeshkumar\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How many create cookies in JavaScript? - scmGalaxy","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How many create cookies in JavaScript? - scmGalaxy","og_description":"JavaScript Cookies Cookies are exposed as the cookie property of the Document object. This property is both readable and writeable. [&hellip;]","og_url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/","og_site_name":"scmGalaxy","article_published_time":"2021-08-14T12:39:32+00:00","article_modified_time":"2022-04-13T16:30:44+00:00","author":"rajeshkumar","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rajeshkumar","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/"},"author":{"name":"rajeshkumar","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"headline":"How many create cookies in JavaScript?","datePublished":"2021-08-14T12:39:32+00:00","dateModified":"2022-04-13T16:30:44+00:00","mainEntityOfPage":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/"},"wordCount":353,"commentCount":0,"keywords":["chrome","Cookies","document object","Google","Javascript","property"],"articleSection":["JavaScript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/","url":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/","name":"How many create cookies in JavaScript? - scmGalaxy","isPartOf":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website"},"datePublished":"2021-08-14T12:39:32+00:00","dateModified":"2022-04-13T16:30:44+00:00","author":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361"},"breadcrumb":{"@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/how-many-create-cookies-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.scmgalaxy.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"How many create cookies in JavaScript?"}]},{"@type":"WebSite","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#website","url":"https:\/\/www.scmgalaxy.com\/tutorials\/","name":"scmGalaxy","description":"A Platform for Software Engineers","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.scmgalaxy.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.scmgalaxy.com\/tutorials\/#\/schema\/person\/a40e0dd38feee7c2779460a804d49361","name":"rajeshkumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/82337e1fc8642c7f36795aa0775af055a4f38882994004b593efd31758d4434e?s=96&d=mm&r=g","caption":"rajeshkumar"},"url":"https:\/\/www.scmgalaxy.com\/tutorials\/author\/rajeshkumar\/"}]}},"_links":{"self":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7946","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/comments?post=7946"}],"version-history":[{"count":1,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7946\/revisions"}],"predecessor-version":[{"id":7954,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/posts\/7946\/revisions\/7954"}],"wp:attachment":[{"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/media?parent=7946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/categories?post=7946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scmgalaxy.com\/tutorials\/wp-json\/wp\/v2\/tags?post=7946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}