Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

113
Views
Create unique identifier with custom format using Plain Javascript

I know that the format of uuid is XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. But I dont want to create a uuid, I want to create a unique identifier with a different format such as XXX-XXXX-XXX.

How do I do that using plain javascript and no libraries.

And is there any way to ensure that the identifier will be unique?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This is an example of how to create a unique fake uuid looking string. Note that this is not a real uuid.

let uuids = [];

function getRandomUuidChar(count = 1) {
  // valid uuid chars: 0-9, a-f, A-F
  const chars = "1234567890abcdefABCDEF".split('');
  let retval = "";
  for (let i = 0; i < count; i++) {
    const randomIndex = Math.floor(Math.random() * chars.length);
    retval += chars[randomIndex];
  }
  return retval;
}

function generatePseudoUuid() {
  //  XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
  const charCounts = [8, 4, 4, 4, 12];
  const uuid = charCounts
    .reduce((retval, count) => {
      retval.push(getRandomUuidChar(count));
      return retval;
    }, [])
    .join("-");
  if (!uuids.includes(uuid)) {
    uuids.push(uuid);
    return uuid;
  }
  generateUuid();
}
for(let i=0; i<10; i++){
  console.log("uuid", generateUuid());
}
console.log(uuids);

output:

uuid 05D9F4C4-ddd8-9ff4-B5F3-c834E5da6F64
uuid 10dc0BCb-1DfD-8efB-EA7c-3f056930315C
uuid 8e5a21C0-EDD9-f3bc-bEBb-bbADe542342E
uuid b484Ca84-Ce89-eb91-94F4-8216b86A7245
uuid De70ABB8-D4E9-DBba-7783-b8109E1b5E27
uuid F1BF66b8-5458-B8d1-DA0c-6BceBea2ccc0
uuid aEffE093-faf8-FBA2-bb88-08865b446412
uuid a3AabDBC-2B4A-AcBc-8afF-8c0deCCeA92C
uuid C7b4DFaA-Da41-041a-FFfE-28064ABF5FB7
uuid E3F2F592-3AEc-aBdD-AC9c-304Dc93ceDa9
[
  '05D9F4C4-ddd8-9ff4-B5F3-c834E5da6F64',
  '10dc0BCb-1DfD-8efB-EA7c-3f056930315C',
  '8e5a21C0-EDD9-f3bc-bEBb-bbADe542342E',
  'b484Ca84-Ce89-eb91-94F4-8216b86A7245',
  'De70ABB8-D4E9-DBba-7783-b8109E1b5E27',
  'F1BF66b8-5458-B8d1-DA0c-6BceBea2ccc0',
  'aEffE093-faf8-FBA2-bb88-08865b446412',
  'a3AabDBC-2B4A-AcBc-8afF-8c0deCCeA92C',
  'C7b4DFaA-Da41-041a-FFfE-28064ABF5FB7',
  'E3F2F592-3AEc-aBdD-AC9c-304Dc93ceDa9'
]
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!