First of all to introduce JS to generate 26 uppercase and lowercase letters, mainly used to Str.charcodeat () and String.fromCharCode () method
1. Use charCodeAt () to get the Unicode encoding of a specific character in the string.
2,fromCharCode () can accept one (or more) of the specified Unicode value, and then return the corresponding string.
The Unicode value for the generated uppercase A is the Generatebig_1 function () {var str = [];
for (Var i=65;i<91;i++) {Str.push (String.fromCharCode (i));
return str;
The Unicode value for uppercase A is generated as the Generatesmall_1 function () {var str = [];
for (Var i=97;i<123;i++) {Str.push (String.fromCharCode (i));
return str;
//Convert string to Unicode code function tounicode (str) {var codes = [];
for (Var i=0;i<str.length;i++) {Codes.push (Str.charcodeat (i));
return codes;
function Generatesmall () {var ch_small = ' a ';
var str_small = ';
for (Var i=0;i<26;i++) {Str_small + = String.fromCharCode (ch_small.charcodeat (0) +i);
return str_small;
function Generatebig () {var ch_big = ' A ';
var str_big = ';
for (Var i=0;i<26;i++) {Str_big + = String.fromCharCode (ch_big.charcodeat (0) +i);
return str_big;
} console.log (Generatebig ());
Console.log (Generatesmall ());
Console.log (Tounicode (Generatebig ()));
Console.log (Tounicode (Generatesmall ()));
Console.log (Generatebig_1 ()); Console.log (Generatesmall_1 ());
Here to introduce JS randomly generated 26 uppercase and lowercase letters, key line code:
function Getcharacter (flag) {
var character= "";
if (flag=== "lower") {
character = String.fromCharCode (Math.floor (Math.random () *26) + "a". charCodeAt (0));
if (flag=== "Upper") {
character = String.fromCharCode (Math.floor (Math.random () *26) + "A". charCodeAt (0));
return character;
}
function Getuppercharacter () {return
getcharacter ("Upper");;
}
function Getlowercharacter () {return
getcharacter ("lower");;
}
Console.log (Getuppercharacter ());
Console.log (Getlowercharacter ());
The code above enables us to randomly output uppercase letters or small letters, and the principle is very simple, using an interval of uppercase or lowercase Unicode codes.
Code two:
/**
* Returns a random lowercase letter *
/
function Getlowercharacter () {return
getcharacter ("lower");
/**
* Returns a random capital letter *
/
function Getuppercharacter () {return
getcharacter ("Upper");
/**
* Returns a letter
/function Getcharacter (flag) {
var character = "";
if (flag = = "Lower") {
character = String.fromCharCode Math.floor (math.random () *) + "a". charCodeAt (0));
if (flag = = "Upper") {
character = String.fromCharCode (Math.floor (Math.random () *) + "A". charCodeAt (0)); c22/>} return
character
}
This article mainly describes how to use JavaScript to achieve output random uppercase or lowercase letters, hoping to bring more or less help.