How to log unicode characters to the console in JavaScript?
Asked Answered
T

5

17

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?

Tales answered 2/7, 2015 at 21:8 Comment(2)
What's your environment and which console are you talking about (chrome, firefox, terminal, ...)?Apron
@Codespawner I tested it in Opera, Firefox, Chrome. Happens in Win 8.1 and OSX too, so we can say this is general.Tales
T
10

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

<meta charset="UTF-8">
Tales answered 3/7, 2015 at 0:23 Comment(1)
And this is why at least Firefox will display a warning on the console if you forget that tag. The character encoding of the HTML document was not declared. …Hussy
P
14

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

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

Second way - use unidecode npm package.

Postobit answered 26/4, 2018 at 8:0 Comment(1)
Sadly, neither of these worked for me in regards to this question: #62728331Borisborja
T
10

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

<meta charset="UTF-8">
Tales answered 3/7, 2015 at 0:23 Comment(1)
And this is why at least Firefox will display a warning on the console if you forget that tag. The character encoding of the HTML document was not declared. …Hussy
A
5

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

<script src="script.js" type="text/javascript" charset="utf-8"/>
Apron answered 2/7, 2015 at 21:35 Comment(0)
W
0

No, you must use this table UNICODE en internet, for example, with the character: 'Ã' do this:

string.replace(/\u00c3/g, 'é'); //And you can change all the symbols by what you want

This is with the first character you don't want and so so with the other characters

à == u00c3 and UNICODE

Worcester answered 15/2, 2022 at 1:57 Comment(0)
G
0
  • At first, the source file must be encoded UTF-8.

  • You have to put chcp 65001 for UTF-8 console encoding.

  • Explore the character hexcode. The full understanding of unicode character form is there: unicode I am using the \u{HEX} form, it is universal.

  • Then use hex and template for evaluate the character.

test #1

var chr={hex:"03A9"} //Ω
chr.dec=parseInt("0x"+chr.hex,16);

chr.htmlx=String.fromCharCode(38)+"#x"+chr.hex+";";
chr.htmld=String.fromCharCode(38)+"#"+chr.dec+";";
chr.unicode="`"+String.fromCharCode(92)+"u{"+chr.hex+"}`";
chr.grafema=eval(chr.unicode);

console.log(JSON.stringify(chr,0,1));
console.log("unicode", chr.unicode);
console.log("decimal",String.fromCharCode(chr.dec));
console.log("grafema", chr.grafema);

Result:

{
 "hex": "03A9",
 "dec": 937,
 "htmlx": "&#x03A9;",
 "htmld": "&#937;",
 "unicode": "`\\u{03A9}`",
 "grafema": "Ω"
}
unicode `\u{03A9}`
decimal Ω
grafema Ω

The String.fromCharCode() works. test #2

var chr={hex:"1F353"} //🍓
chr.dec=parseInt("0x"+chr.hex,16);

chr.htmlx=String.fromCharCode(38)+"#x"+chr.hex+";";
chr.htmld=String.fromCharCode(38)+"#"+chr.dec+";";
chr.unicode="`"+String.fromCharCode(92)+"u{"+chr.hex+"}`";
chr.grafema=eval(chr.unicode);

console.log(JSON.stringify(chr,0,1));
console.log("unicode", chr.unicode);
console.log("decimal",String.fromCharCode(chr.dec));
console.log("grafema", chr.grafema);

{
 "hex": "1F353",
 "dec": 127827,
 "htmlx": "&#x1F353;",
 "htmld": "&#127827;",
 "unicode": "`\\u{1F353}`",
 "grafema": "🍓"
}
unicode `\u{1F353}`
decimal 
grafema 🍓

The String.fromCharCode() does not works, due to greater than 0xFFFF has truncated.

Greenockite answered 9/7 at 9:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.