str.random()

random character in string

๐Ÿ”ฐ JS โŸฉ Types โŸฉ String โŸฉ methods

// โญ๏ธ string.random()
// โญ๏ธ Note: this may not work well with Unicodeโ—๏ธ 
String.prototype.random = function(){
    return this[randomInt(this.length)];
}

// โญ๏ธ randomChar(str)
// โญ๏ธ Note: this may not work well with Unicodeโ—๏ธ
export function randomChar(str) {
    return str[randomInt(0, str.length - 1)];
}

Last updated

Was this helpful?