Skip to content
Advertisement

How to iterate over over all Unicode characters?

Is it possible to iterate over all Unicode characters (UTF-8)? Thanks! I’ve tried using:

character = String.fromCharCode(i);

But I’m not sure how to implement it.

Advertisement

Answer

According to the docs, the parameter passed to String.fromCharCode(a) is converted calling ToUint16 and then said character is returned. You may call it with any number you want but the values will be capped to between 0 and 216 or 232

highNumber = 500; //This could go very high
out = ""
for(i=0;i<highNumber;i++){
    out += String.fromCharCode(i);
}
console.log(out);

Danger note if you run this code using 2^16you may freeze your tab or browser, it’s way too big. This is understanding you want to iterate over all characters and not all characters in a given string which is quite a different thing.

A sample output of a more reasonable highNumber(ie 500) is the following:

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqr
stuvwxyz{|}~ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæç
èéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺ
ĻļĽľĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœŔŕŖŗŘřŚśŜŝŞşŠšŢţŤťŦŧŨũŪūŬŭŮůŰűŲųŴŵŶŷŸŹźŻżŽžſƀƁƂƃƄƅƆƇƈƉƊƋƌƍ
ƎƏƐƑƒƓƔƕƖƗƘƙƚƛƜƝƞƟƠơƢƣƤƥƦƧƨƩƪƫƬƭƮƯưƱƲƳƴƵƶƷƸƹƺƻƼƽƾƿǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟǠ
ǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯǰDZDzdz
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement