Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js generate random string of length

// Naive solution (not recommended)
Math.random().toString(36).slice(2, 7);

// Option 1 (native node js)
var crypto = require("crypto");
var id = crypto.randomBytes(20).toString('hex');

// Option 2 (3rd party module)
var uuid = require("uuid");
var id = uuid.v4();

// Option 3 (browser native)
// dec2hex :: Integer -> String
// i.e. 0-255 -> '00'-'ff'
function dec2hex (dec) {
  return dec.toString(16).padStart(2, "0")
}

// generateId :: Integer -> String
function generateId (len) {
  var arr = new Uint8Array((len || 40) / 2)
  window.crypto.getRandomValues(arr)
  return Array.from(arr, dec2hex).join('')
}
Comment

js generate random string of length

function makeid(length) {
  var result = '';
  var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  var charactersLength = characters.length;
  for (var i = 0; i < length; i++) {
    result += characters.charAt(Math.floor(Math.random() *
                                           charactersLength));
  }
  return result;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: immediately invoked function expression async 
Javascript :: json stringify close circle 
Javascript :: how to change root variable css 
Javascript :: emailjs react npm 
Javascript :: change port react app 
Javascript :: How to fetch API data using POST and GET in PHP 
Javascript :: javascript form post json data 
Javascript :: js create array from for loop 
Javascript :: javascript toisostring without milliseconds 
Javascript :: find input by value attribute javascript 
Javascript :: react footer 
Javascript :: on refresh react url not working or 404 page not showing react 
Javascript :: javascript random number between 20 and 30 
Javascript :: array map destructuring 
Javascript :: if else dart 
Javascript :: js array comprehension 
Javascript :: javascript capitalize 
Javascript :: javascript set time to start of day 12 am 
Javascript :: axios send post to php 
Javascript :: convert an array to string javascript 
Javascript :: duplicate numbers in an array javascript 
Javascript :: do while javascript 
Javascript :: print random string from an array to screen in javascript 
Javascript :: react router dom private route 
Javascript :: how to get folder names with fs 
Javascript :: html get class property 
Javascript :: how to remove key value pair from object in javascript 
Javascript :: javascript set input value 
Javascript :: javascript random number up to including 2 
Javascript :: javascript document.createElement add function 
ADD CONTENT
Topic
Content
Source link
Name
2+1 =