Variable Names and Character Encodings

I have checked that Ο€ valid for a JavaScript variable name. Also, when I try to write var Ο€ = Math.PI in the Chrome console, everything is fine.

However, when I include the var Ο€ = Math.PI inside a .js file written using Sublime Text 2 on a Mac, Chrome complains:

 Uncaught SyntaxError: Unexpected token ILLEGAL 

I tried the encodings UTF-8, UTF-16 LE and UTF-16 BE, but no one works. When I execute the console.log('Ο€') written inside Sublime Text 2 with UTF-8, Chrome prints:

 π 

How can I use the JavaScript Ο€ variable name in Sublime Text 2 so that Chrome understands this?

+7
source share
1 answer

Make sure you set <meta charset="utf-8"> in the header of your html or if the encoding is different, you can set the charset attribute in the script tag:

 <script src="script.js" charset="utf-8"> 
+6
source

All Articles