Making random and unique ID with Javascript

Last week, I needed to find a way to generate a unique id to get names for phone files on ios systems. Anyway, googling around, I found this simple function.

All you have to do is call it, where you need to generate the id, and pass the desired length…


This content originally appeared on DEV Community and was authored by Maria Antonella ?

Last week, I needed to find a way to generate a unique id to get names for phone files on ios systems. Anyway, googling around, I found this simple function.

All you have to do is call it, where you need to generate the id, and pass the desired length of the id.
And magic! It returns an id made with characters and numbers (in this example, of course!)

const makeRandomId= (length) => {
      let result = ''
      const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
      for (let i = 0; i < length; i++ ) {
        result += characters.charAt(Math.floor(Math.random() * characters.length));
     }
     return result;
  }

? charAt: The charAt() method returns the character at a specified index in a string.
? floor(): The floor() method rounds a number DOWNWARDS to the nearest integer, and returns the result.
? random(): Math.random() returns a random number between 0 (inclusive), and characters.length (exclusive):

Math.random() used with Math.floor() can be used to return random integers.

That's all! :)


This content originally appeared on DEV Community and was authored by Maria Antonella ?


Print Share Comment Cite Upload Translate Updates
APA

Maria Antonella ? | Sciencx (2021-09-15T17:27:49+00:00) Making random and unique ID with Javascript. Retrieved from https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/

MLA
" » Making random and unique ID with Javascript." Maria Antonella ? | Sciencx - Wednesday September 15, 2021, https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/
HARVARD
Maria Antonella ? | Sciencx Wednesday September 15, 2021 » Making random and unique ID with Javascript., viewed ,<https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/>
VANCOUVER
Maria Antonella ? | Sciencx - » Making random and unique ID with Javascript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/
CHICAGO
" » Making random and unique ID with Javascript." Maria Antonella ? | Sciencx - Accessed . https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/
IEEE
" » Making random and unique ID with Javascript." Maria Antonella ? | Sciencx [Online]. Available: https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/. [Accessed: ]
rf:citation
» Making random and unique ID with Javascript | Maria Antonella ? | Sciencx | https://www.scien.cx/2021/09/15/making-random-and-unique-id-with-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.