Get random strings from given string In javascript

javascript
random
string

The below function return a random string with given length

function randomString(length) {
    chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    var result = '';
    for (var i = length; i > 0; --i) result += chars[Math.round(Math.random() * (chars.length - 1))];
    return result;
}
var res = randomString(4);
alert(res);

 

You might also like:

Functions in javascript

25-05-2018 functions javascript es5 es6 arrow functions named functions Closures Nested Functions Callbacks Arrow functions.

Example for Conditional Operator in php

16-04-2017 php Conditional Operator example

How to get id by using name attribute in javascript

20-03-2017 html id name javascript

How to set or update or push the values into Object in javacript

27-02-2017 javascript set update push object

Get Unique values from array/object in javascript

19-02-2017 javascript unique remove repeated values from array