How to log unicode characters to the console in JavaScript?

試著忘記壹切 提交于 2019-12-08 17:01:02

问题


I have a hungarian statement that I would like to log to the console like this:

console.log('Probléma a működésben.');

But it prints the following:

Probléma a működésben.

The non ASCII characters are messed up, but I don't think the reason for this is that the console doesn't support unicode characters, because if I paste the log straight into the console it produces the proper output.

I tested it in Opera, Firefox, Chrome. Happens in Win 8.1 and OSX too, so we can say this is general.

Are there other things that should persist for the proper unicode console logging, like HTML charset or file encode?


回答1:


First way: you have to find your characters in unicode table

console.log( '\u03A9' ); // Ω

Second way - use unidecode npm package.




回答2:


You can specify the encoding of your file by setting the charset attribute

<script src="script.js" type="text/javascript" charset="utf-8"/>



回答3:


I found out that if you set the proper charset in a <meta> tag in the <head> it will work:

<meta charset="UTF-8">


来源:https://stackoverflow.com/questions/31194677/how-to-log-unicode-characters-to-the-console-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!