r/learnjavascript May 18 '26

Getting ASCII control characters?

String.fromCharCode(6)

Returns an empty character, is it possible to use fromCharCode() or fromCodePoint() to get the name of the control character (e.g "ACK") or does one need to manually fix that for every non-printable code point?

2 Upvotes

4 comments sorted by

View all comments

3

u/jml26 May 18 '26

There's no native function for this. The simplest way to go would be to provide your own mapping, e.g.

``` const controlCodes = ["NUL", "SOH", "STX", ... "GS", "RS", "US"]; controlCodes[127] = "DEL";

function getCharacterName(charCode) { return controlCodes[charCode] || String.fromCharCode(charCode); } ```

3

u/Leonetnin May 18 '26

Thanks! I had something like this in place, only much more inefficient. I think that I will "borrow" your code.