{"id":2388,"date":"2017-03-27T17:56:01","date_gmt":"2017-03-27T12:26:01","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=2388"},"modified":"2018-10-24T10:14:04","modified_gmt":"2018-10-24T04:44:04","slug":"create-guid-uuid-javascript","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/create-guid-uuid-javascript\/","title":{"rendered":"Create GUID  UUID in JavaScript"},"content":{"rendered":"<h3 id=\"guid\"><span style=\"color: #800000;\"><b>GUID :<\/b><\/span><\/h3>\n<p>A <strong>GUID<\/strong>\u00a0(Global Unique Identifier) is Microsoft\u2019s implementation of a unique identifier\u00a0<strong>(UUID)<\/strong>.<\/p>\n<ul>\n<li>In its simplest form, Guid lets we generate raw <strong>GUID<\/strong> formatted <a href=\"https:\/\/www.wikitechy.com\/technology\/convert-string-integer-javascript\/\" target=\"_blank\" rel=\"noopener\">strings<\/a>:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;Guid.raw()%3B%0A%2F%2F%20-%3E%20&#8217;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>Let&#8217;s generate a new Guid instance.<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;var%20guid%20%3D%20Guid.create()%3B%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<ul>\n<li>We&#8217;ve now got an <a href=\"https:\/\/www.wikitechy.com\/technology\/detecting-undefined-object-property\/\" target=\"_blank\" rel=\"noopener\">object<\/a> which we can work with programmatic manner. Lets check the validity of our Guid using the built-in validator :<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;Guid.isGuid(guid)%3B%0A%2F%2F%20-%3E%20true%0AGuid.value%3B%0A%2F%2F%20-%3E%20&#8217;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>A handy bit of functionality is that its <a href=\"https:\/\/www.wikitechy.com\/technology\/java-programming-print-possible-strings-can-made-placing-spaces\/\" target=\"_blank\" rel=\"noopener\">toString<\/a> method returns the string value.<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;var%20itemUrl%20%3D%20%22http%3A%2F%2Fwhatever.com%2Fitems%2F%22%20%2B%20guid%3B%0A%2F%2F%20-%3E%20&#8217;http%3A%2F%2Fwhatever.com%2Fitems%2F6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>If we need a placeholder Guid, or a value to represent a non-GUID, use the <a href=\"https:\/\/www.wikitechy.com\/technology\/java-inner-class-static-nested-class\/\" target=\"_blank\" rel=\"noopener\">static<\/a> EMPTY property:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;Guid.EMPTY%3B%0A%2F%2F%20-%3E%20&#8217;00000000-0000-0000-0000-000000000000&#8217;%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>Once we have a Guid object, we can&#8217;t change its value:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;guid.value%20%3D%20%22go%20suck%20it%2C%20guid!%22%0Aguid.value%3B%0A%2F%2F%20-%3E%20&#8217;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d%E2%80%98%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>To instantiate an Guid object using an existing GUID string, use the <a href=\"https:\/\/www.wikitechy.com\/technology\/java-constructor-tutorial-code-examples\/\" target=\"_blank\" rel=\"noopener\">constructor<\/a>:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;var%20guid%20%3D%20new%20Guid(&#8216;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;)%3B%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>we can check the equality of two different Guid objects using the <b>equals<\/b> instance method.<\/li>\n<li>Compare a Guid object to a GUID string:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;guid.equals(&#8216;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;)%3B%0A%2F%2F%20-%3E%20true%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>Compare two Guid objects:<\/li>\n<\/ul>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;guid.equals(new%20Guid(&#8216;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;))%3B%0A%2F%2F%20-%3E%20true%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"commonjs-module\"><span style=\"color: #0000ff;\"><b>CommonJS Module:<\/b><\/span><\/h3>\n<p>&nbsp;<\/p>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;exports.generate%20%3D%20function()%0A%7B%0A%20%20%20%20var%20guid%20%3D%20&#8217;xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx&#8217;.replace(%2F%5Bxy%5D%2Fg%2C%20function(c)%20%0A%7B%0A%20%20%20%20%20%20%20%20var%20r%20%3D%20Math.random()*16%7C0%2C%20v%20%3D%20c%20%3D%3D%20&#8217;x&#8217;%20%3F%20r%20%3A%20(r%260&#215;3%7C0x8)%3B%0A%20%20%20%20%20%20%20%20%20return%20v.toString(16)%3B%0A%20%20%20%20%7D)%3B%20%20%20%20%20%0A%20%20%20%20return%20guid%3B%0A%7D%3B%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<h2 id=\"uuid\"><span style=\"color: #800080;\"><b>UUID:<\/b><\/span><\/h2>\n<ul>\n<li>\u201c<strong>Universally Unique Identifier<\/strong> (UUID) is an identifier standard used in software construction, standardized by the Open Software Foundation (OSF) as part of the Distributed Computing Environment (DCE).\u201d<\/li>\n<li>A UUID is a <strong>16-octet<\/strong> (128-bit) number.<\/li>\n<li>A UUID is represented by 32 lowercase hexadecimal digits and it displayed in 5 groups separated by hyphens(-).<\/li>\n<li>This represent in form of 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and four hyphens).<\/li>\n<li>Simple, fast generation of\u00a0<b>RFC4122<\/b>\u00a0UUIDS.<\/li>\n<\/ul>\n<h3 id=\"commonjs-module-2\"><span style=\"color: #0000ff;\"><b>CommonJS Module:<\/b><\/span><\/h3>\n<p><span style=\"color: #000000;\"><b>npm<\/b><b> install uuid<\/b><\/span><\/p>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;%2F%2F%20Generate%20a%20v1%20UUID%20(time-based)%20%0Aconst%20uuidV1%20%3D%20require(&#8216;uuid%2Fv1&#8217;)%3B%0AuuidV1()%3B%20%2F%2F%20-%3E%20&#8217;6c84fb90-12c4-11e1-840d-7b25c5ee775a&#8217;%20%0A%20%2F%2F%20Generate%20a%20v4%20UUID%20(random)%20%0Aconst%20uuidV4%20%3D%20require(&#8216;uuid%2Fv4&#8217;)%3B%0AuuidV4()%3B%20%2F%2F%20-%3E%20&#8217;110ec58a-a0f2-4ac4-8393-c866d813b8d1&#8217;%20%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;uuid.v1(%5Boptions%20%5B%2C%20buffer%20%5B%2C%20offset%5D%5D%5D)%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>To Generate and return a RFC4122 v1 (timestamp-based) UUID.<\/li>\n<li><span style=\"color: #000000;\"><b>options <\/b><\/span>&#8211; (Object) Optional uuid state to apply.<\/li>\n<\/ul>\n<h3 id=\"properties-may-include-the-following\"><span style=\"color: #003366;\"><b>Properties may include the following:<\/b><\/span><\/h3>\n<ul>\n<li><span style=\"color: #000000;\"><b>node<\/b> &#8211;<\/span> (Array) Node id as Array of 6 bytes . Default: Randomly generated ID.<\/li>\n<li><span style=\"color: #000000;\"><b>clockseq<\/b><\/span> &#8211; (Number between 0 &#8211; 0x3fff) RFC clock sequence. Default: An internally maintained clockseq is used.<\/li>\n<li><span style=\"color: #000000;\"><b>msecs<\/b><\/span> &#8211; (Number | Date) Time in milliseconds since unix Epoch. Default: The current time is used.<\/li>\n<li><span style=\"color: #000000;\"><b>nsecs<\/b><\/span> &#8211; (Number between 0-9999) additional time, in 100-nanosecond units. Ignored if msecs is unspecified.<\/li>\n<li><span style=\"color: #000000;\"><b>Default<\/b>:<\/span> internal uuid counter is used.<\/li>\n<li><span style=\"color: #000000;\"><b>buffer<\/b><\/span> &#8211; (Array | Buffer) Array or buffer where UUID bytes are to be written.<\/li>\n<li><span style=\"color: #000000;\"><b>offset<\/b><\/span> &#8211; (Number) Starting index in buffer at which to begin writing.<\/li>\n<\/ul>\n<h3 id=\"example1-generate-string-uuid-with-fully-specified-options\"><span style=\"color: #ff00ff;\"><strong>Example1<\/strong>: <\/span>Generate string UUID with fully-specified options<\/h3>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;uuid.v1(%0A%7B%0A%20%20node%3A%20%5B0x01%2C%200&#215;23%2C%200&#215;45%2C%200&#215;67%2C%200&#215;89%2C%200xab%5D%2C%0A%20%20clockseq%3A%200&#215;1234%2C%0A%20%20msecs%3A%20new%20Date(&#8216;2011-11-01&#8217;).getTime()%2C%0A%20%20nsecs%3A%205678%0A%7D%0A)%3B%20%20%20%2F%2F%20-%3E%20%22710b962e-041c-11e1-9234-0123456789ab%22%20%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"example2-in-place-generation-of-two-binary-ids\"><span style=\"color: #ff00ff;\"><b>Example2<\/b>:<\/span> In-place generation of two binary IDs<\/h3>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;%2F%2F%20Generate%20two%20ids%20in%20an%20array%20%0Aconst%20arr%20%3D%20new%20Array(32)%3B%20%2F%2F%20-%3E%20%5B%5D%20%0Auuid.v1(null%2C%20arr%2C%200)%3B%20%20%20%2F%2F%20-%3E%20%5B02%20a2%20ce%2090%2014%2032%2011%20e1%2085%2058%200b%2048%208e%204f%20c1%2015%5D%20%0Auuid.v1(null%2C%20arr%2C%2016)%3B%20%20%2F%2F%20-%3E%20%5B02%20a2%20ce%2090%2014%2032%2011%20e1%2085%2058%200b%2048%208e%204f%20c1%2015%2002%20a3%201c%20b0%2014%2032%2011%20e1%2085%2058%200b%2048%208e%204f%20c1%2015%5D%20%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;uuid.v4(%5Boptions%20%5B%2C%20buffer%20%5B%2C%20offset%5D%5D%5D)%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<ul>\n<li>To Generate and return a RFC4122 v4 UUID.<\/li>\n<li><span style=\"color: #000000;\"><b>options<\/b><\/span> &#8211; (Object) Optional uuid state to apply.<\/li>\n<\/ul>\n<h3 id=\"properties\"><span style=\"color: #003366;\"><b>Properties:<\/b><\/span><\/h3>\n<ul>\n<li><span style=\"color: #000000;\"><b>random<\/b> &#8211; (Number[16]) Array of 16 numbers (0-255) to use in place of randomly generated values<\/span><\/li>\n<li><span style=\"color: #000000;\"><b>rng<\/b> \u2013<\/span>\n<ul>\n<li><span style=\"color: #000000;\">(Function) Random # generator to use.<\/span><\/li>\n<li><span style=\"color: #000000;\">Set to one of the built-in generators &#8211; uuid.mathRNG (all platforms), uuid.nodeRNG (node.js only), uuid.whatwgRNG (WebKit only) &#8211; or a custom function that returns an array[16] of byte values.<\/span><\/li>\n<\/ul>\n<\/li>\n<li><span style=\"color: #000000;\"><b>buffer<\/b> &#8211; (Array | Buffer) <a href=\"https:\/\/www.wikitechy.com\/technology\/cc-programming-maximum-of-all-subarrays-of-size-k\/\" target=\"_blank\" rel=\"noopener\">Array<\/a> or buffer where UUID bytes are to be written.<\/span><\/li>\n<li><span style=\"color: #000000;\"><b>offset <\/b>&#8211; (Number) Starting index in buffer at which to begin writing.<\/span><\/li>\n<\/ul>\n<h3 id=\"example1-generate-string-uuid-with-fully-specified-options-2\"><span style=\"color: #000000;\"><span style=\"color: #ff00ff;\"><b>Example1: <\/b><\/span>Generate string UUID with fully-specified options<\/span><\/h3>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;uuid.v4(%0A%7B%0A%20%20random%3A%20%5B%0A%20%20%20%200&#215;10%2C%200&#215;91%2C%200&#215;56%2C%200xbe%2C%200xc4%2C%200xfb%2C%200xc1%2C%200xea%2C%0A%20%20%20%200&#215;71%2C%200xb4%2C%200xef%2C%200xe1%2C%200&#215;67%2C%200x1c%2C%200&#215;58%2C%200&#215;36%0A%20%20%5D%0A%7D%0A)%3B%0A%2F%2F%20-%3E%20%22109156be-c4fb-41ea-b1b4-efe1671c5836%22%20%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"example2-generate-two-ids-in-a-single-buffer\"><span style=\"color: #000000;\"><span style=\"color: #ff00ff;\"><b>Example2: <\/b><\/span>Generate two IDs in a single buffer<\/span><\/h3>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;const%20buffer%20%3D%20new%20Array(32)%3B%20%2F%2F%20(or%20&#8217;new%20Buffer&#8217;%20in%20node.js)%20%0Auuid.v4(null%2C%20buffer%2C%200)%3B%0Auuid.v4(null%2C%20buffer%2C%2016)%3B%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h2 id=\"how-to-create-a-guid-uuid-in-javascript\"><span style=\"color: #800080;\"><b>How to Create a GUID \/ UUID in Javascript<\/b><\/span><\/h2>\n<p><b>\u00a01.<\/b>To create an rfc4122 version 4 compliant guid<\/p>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;function%20createGuid()%0A%7B%0A%20%20%20%20return%20&#8217;xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx&#8217;.replace(%2F%5Bxy%5D%2Fg%2C%20function(c)%0A%20%7B%0A%20%20%20%20%20%20%20%20var%20r%20%3D%20Math.random()*16%7C0%2C%20v%20%3D%20c%20%3D%3D%3D%20&#8217;x&#8217;%20%3F%20r%20%3A%20(r%260&#215;3%7C0x8)%3B%0A%20%20%20%20%20%20%20%20return%20v.toString(16)%3B%0A%20%20%20%20%7D)%3B%0A%7D%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<h3 id=\"usage\"><span style=\"color: #339966;\"><b>Usage:<\/b><\/span><\/h3>\n[pastacode lang=&#8221;javascript&#8221; manual=&#8221;var%20uuid%20%3D%20createGuid()%3B%0A%3E%3E%3E%20%22e2ece964-0315-4b91-b411-20f9868ce7d4%22%0A&#8221; message=&#8221;javascript code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>GUID : A GUID\u00a0(Global Unique Identifier) is Microsoft\u2019s implementation of a unique identifier\u00a0(UUID). In its simplest form, Guid lets we generate raw GUID formatted strings: [pastacode lang=&#8221;javascript&#8221; manual=&#8221;Guid.raw()%3B%0A%2F%2F%20-%3E%20&#8217;6fdf6ffc-ed77-94fa-407e-a7b86ed9e59d&#8217;%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/] Let&#8217;s generate a new Guid instance. [pastacode lang=&#8221;javascript&#8221; manual=&#8221;var%20guid%20%3D%20Guid.create()%3B%0A&#8221; message=&#8221;JAVASCRIPT CODE&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/] [ad type=&#8221;banner&#8221;] We&#8217;ve now got an object which we can [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[5648,5643,5638,5635,5649,5637,5642,5641,5639,5647,5645,5651,5644,5646,5650,5640,5636],"class_list":["post-2388","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-angular-guid","tag-create-a-guid-uuid-in-javascript","tag-create-a-guid-in-javascript","tag-create-guid-uuid-in-javascript","tag-generate-guid-java","tag-generate-guid-using-javascript","tag-generating-guids-in-javascript","tag-guid","tag-how-to-create-a-guid-uuid-in-javascript","tag-javascript-generate-unique-number","tag-javascript-uuid-library","tag-jquery-generate-unique-id","tag-jquery-guid","tag-nodejs-guid","tag-react-uuid","tag-uuid","tag-uuid-generator"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=2388"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2388\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=2388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=2388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=2388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}