Print A to Z in Javascript


Print A to Z in Javascript


Hi All! Today we'll be covering How to print A to Z in Javascript? i.e., print all the characters of the English alphabet(A-Z). 
 
Javascript's String object has an inbuilt method fromCharCode() that takes an ASCII code as input and returns the corresponding character.

We are looping through ASCII codes from 65(A) to 90(Z) by converting A to its ASCII equivalent with the help of  "A".charCodeAt(0) inside a for() loop.
And then printing the characters from A-Z using String.fromCharCode(i).  

 <!DOCTYPE html>  
 <html>  
   <head>  
     <script>  
       function print() {  
         for(var i = "A".charCodeAt(0); i <= "Z".charCodeAt(0); i++) {  
         document.getElementById("list").insertAdjacentHTML('beforeend','<li>'+String.fromCharCode(i)+'</li>')  
         }  
       }  
     </script>  
   </head>  
 <body>  
   <button onclick="print()">Print A to Z</button>  
   <ul id="list">  
     <!-- here we will insert <li> dynamically -->  
   </ul>  
 </body>  
 </html>  

Output :