Why are const \ let variables not sandboxed?

  • 0
    const vm = require('vm');
    
    const context = {};
    
    const code = `
      const v1 = 5;
      const v2 = 8;
      var c1 = 4;
      var c2 = 5;
    `;
    
    vm.runInNewContext(code, context);
    
    console.dir(context)


    Why are only c1 and c2 available to me from context? I didn't wrap v1 and v2 in a separate block like this:
    const code = `
      {const v1 = 5;
      const v2 = 8;}
      var c1 = 4;
      var c2 = 5;
    `;

    That is, in fact, v1 and v2 had to fall into the global scope inside the sandbox as v1 and v2, and therefore in the context, no?
    JavaScript Lily Mayo, Jan 7, 2019

  • 1 Answers
  • 0
    In short, the scope affects!

    An article on the differences between let / const / var.
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!