6

I have to print out the letters from A to Z each for itself. So I tried the following:

for(var i = 65; i < 91; i++) {
     $('#alphabet').append('<div class="letter">' + '%' + i + '</div>');
}

My idea is to use the decimal numbers of the letters (for example: 65 - A) to easily print them via loop. Is this possible or do I have to use an array?

Best regards.

Stefan Surkamp
  • 972
  • 1
  • 16
  • 31

2 Answers2

11

You can use String.fromCharCode to convert a character code to string.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46
  • thats exactly what i've been looking for (hours). thank you very much! – Stefan Surkamp Nov 01 '11 at 11:08
  • @Josh Why write the name of Stefan Surkamp in Russian when he/she appears to be a German? Also "Ш" appears to imitate the pronounciation in Deutsche, quite interesting. :) – awllower Jan 11 '15 at 09:43
  • I'm sure I used autocomplete @awllower. I bet he's changed his display name ;-) – Josh Jan 11 '15 at 14:21
  • @Josh I found out that there is such a functionality as auto-complete after using the website for 3 years and 11 months (on math stack exchange). Thanks for that information. ;) – awllower Jan 11 '15 at 15:49
2

For this use (printing letters from A to Z), String.fromCharCode is sufficient; however, the question title specifies Unicode. If you are looking to convert Unicode codes (and if you came here from search, you might), you need to use String.fromCodePoint.

Note, this function is new to ES6/ES2015, and you may need to use a transpiler or polyfill to use it in older browsers.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103