3

Is using a unicode escape sequence valid in the let keyword?

Restating: Is the following code valid?

l\u0065t a = 1;

Is it equivalent to this code:

let a = 1;
programmerjake
  • 1,794
  • 11
  • 15

1 Answers1

3

No.

ECMAScript does allow Unicode escape sequences in string literals, template literals and regex literals (so far so expected), and even in identifier names (more details). It does however not allow them in raw source text1, so they cannot form things such as keywords, other tokens and whitespaces.

(This has not changed since ES5. You also were not allowed, and continue to be not allowed, to use escape sequences in var.)

1: One could of course imagine an implementation that interprets anything, including escape characters, to derive a source text2, but that would be a) weird and b) outside the scope of ECMAScript. Think of passing a string through JSON.parse and eval - such a string might as well contain l\u0065t.
2: An actual example of something like this (where it actually makes sense) are HTML entities in a <script> tag.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • I would change your example to something that's not a reserved word in ES5. The standard specifically states that escape sequences are not allowed in reserved words. – programmerjake May 21 '16 at 20:43
  • @programmerjake: It does? I missed that, can you point me to it? – Bergi May 21 '16 at 20:50
  • see the note in 11.6.2 http://www.ecma-international.org/ecma-262/6.0/#sec-reserved-words – programmerjake May 21 '16 at 20:53
  • @programmerjake: Oh, right, I looked at [ES5 which doesn't contain that note](http://es5.github.io/#x7.6.1). Imo it's implicit that *all* tokens must be literal sequences of the respective source characters. – Bergi May 21 '16 at 21:03
  • @programmerjake: That raises the point though whether `var v\u0061r` would be valid though, if `v\u0061r` is strictly not a reserved word that is forbidden in identifiers :-) I guess they actually meant that reserved words are not allowed in the *string values* of identifiers. – Bergi May 21 '16 at 21:12
  • from what I understand `var v\u0061r;` is valid. They don't check the string value of identifiers. – programmerjake May 21 '16 at 21:13
  • I don't think so (you certainly can't have a variable named `var`, regardless how you expressed the name). Maybe we should discuss this in another question or right at https://bugs.ecmascript.org/ :-) – Bergi May 21 '16 at 21:16