Randomblue
Randomblue

Reputation: 116363

Variable names and character encodings

I have checked that π is valid for a JavaScript variable name. Also, when I try writing var π = Math.PI in the Chrome console, all is fine.

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

Uncaught SyntaxError: Unexpected token ILLEGAL

I have tried the encodings UTF-8, UTF-16 LE and UTF-16 BE, but none work. When I execute the line 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 it?

Upvotes: 7

Views: 2677

Answers (1)

Trevor
Trevor

Reputation: 9578

Check to make sure you are setting a <meta charset="utf-8"> in the head of your html or if the charset is different you can set the charset attribute on your script tag:

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

Upvotes: 6

Related Questions