Search code examples
javascriptvariablesunicodeiso

JavaScript variable name in ISO 8859-1 or Unicode


JavaScript documentations states: Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as å and ü in identifiers.

So I tried this out:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>JavaScript index</title>
    <script>

    var ü = 'tom';

    console.log( ü );

    </script>
</head>
</html>

Console returns an: Uncaught SyntaxError: Unexpected token ILLEGAL

Yes I am in a browser environment: Chrome 40

Why is this an error?


Solution

  • You didn't indicate to the browser what encoding the HTML file was to be parsed with. You should add a <meta charset="..."/> element in the start of the <head> to let it know what encoding you've saved it as. (I suggest using UTF-8.)

    From the error I suspect you saved the file as UTF-8, but without the meta element the browser has arbitrarily guessed the file is in Windows code page 1252 (Western European). The character ü encoded to UTF-8 is the byte sequence 0xC3, 0xBC. That byte sequence mis-decoded as cp1252 is ü, which is an ILLEGAL variable name because ¼ is not a letter.