Arc Forumnew | comments | leaders | submitlogin
1 point by Pauan 4185 days ago | link | parent

Another fun fact: in strict mode, variables created by "eval" are scoped to the call to "eval":

  (function () {
    "use strict";
    console.log(eval("var x = 5; x"));
    console.log(eval("x"));
  })()
The above will first print 5, and will then throw an error saying that "x" is undefined. This provides another technique to create hidden variables.

If the above code wasn't in strict mode, it would have printed 5 both times, and the variable "x" would have been local to the function block:

  (function () {
    console.log(eval("var x = 5; x"));
    console.log(eval("x"));
    return x;
  })()