I have some code like this:
str = ""
for (var i = 0; i < 5; i++) str += eval("'\\u" + Math.floor(Math.random() * 65536).toString(16) + "'")
Occasionally when this is ran in a console, an Uncaught SyntaxError: Invalid Unicode escape sequence
occurs. The error is in the eval
uation. The syntax, including the eval
, should work. A string with a backslash, a "u", and 4 random hex digits is passed into the eval
. The eval
interprets the "\u" and the hex digits as an Unicode character. Somehow, the error appears with that good (enough) syntax.
Please find a the reason behind this error, and present a version of this code that circumvents this error.