I am new to JS, and currently study using Eloquent JS Book. In ch 10 there was a function below. When i run it on localhost with live server, the output is 1 for both logs. However when using the console given by the book or other sandbox js consoles I found online, the output is 2 and 1. If you know why, please help
const x = 1;
function evalAndReturnX(code) {
eval(code);
return x;
}
console.log(evalAndReturnX("var x = 2"));
// → 2
console.log(x);
// → 1