Amit Tomar
Amit Tomar

Reputation: 4858

Printing all ASCII characters in Javascript

I need to do something like this:

  1. Have a variable of some type.
  2. Run in a loop and assign all the possible ASCII characters to this variable and print them, one by one.

Is something similar possible for UNICODE also?

Upvotes: 8

Views: 29214

Answers (3)

Guffa
Guffa

Reputation: 700820

There are some of the ASCII characters that are non-printable, but for example getting the characters from 32 (space) to 126 (~), you would use:

var s = '';
for (var i = 32; i <= 127; i++) s += String.fromCharCode(i);

The unicode character set has more than 110,000 different characters (see Unicode), but a normal font doesn't contain all of them, so you can't display them anyway. You would have to specify what parts of the character space you are interested in.

Upvotes: 1

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201876

Others have shown how to print the printable Ascii characters. It is possible to print all other Ascii characters, too, though they are control characters with system-dependent effect (often no effect). To create a string containing all Ascii characters into a string, you could do this:

var s = '';
for (var i = 0; i <= 127; i++) s += String.fromCharCode(i);

Unicode is much more tricky, because the Unicode coding space, from 0 to 0x10FFFF, contains a large number of unassigned code points as well as code points designated as noncharacters. There are also Private Use code points, which may be used to denote characters by “private agreement” but have no generally assigned meaning. Moreover, many Unicode characters are nonspacing, i.e. meant to combine with the preceding character (e.g., turning “a” to “â”), so you can’t visually print them in a row. There is no simple way in JavaScript to determine, from a integer, the class of the corresponding code point – you might need to read the UnicodeData.txt file, parse it, and use the information there to classify code points.

Finally, there is the programming issue that the JavaScript concept of character corresponds to a 16-bit code unit (not code point), and any Unicode code point larger than 0xFFFF needs to be represented using two code units (so-called surrogates). If you are using JavaScript in the context of an HTML document and you want to print characters in th HTML content, then the simplest way is to use character references like &#x10400; (which denotes the Unicode character at code point 10400 hexadecimal) and assign the string to the innerHTML property of an element.

If you need to write ranges of Unicode characters, you might take a look at the Full Unicode Input utility that I recently wrote. Its source code illustrates some ways of dealing with Unicode characters in JavaScript.

Upvotes: 0

Michael Krelin - hacker
Michael Krelin - hacker

Reputation: 143289

I'm not sure how exactly you want to print, but this will console.log printable ascii

for(var i=32;i<127;++i) console.log(String.fromCharCode(i));

You can document.write then if that's your intention. And if the environment is unicode, it should work for unicode as well, I believe.

Upvotes: 17

Related Questions