Example 1 — Official Global Flag
Create a global variable from inside a function.
function test() {
jQuery.globalEval("var newVar = true;");
}
test();
console.log(typeof newVar); // "boolean"
console.log(newVar); // true How It Works
- Call from a function.
test()shows the code is not running at top level. - Run globally.
jQuery.globalEval("var newVar = true;")definesnewVaron the global object. - After return. The variable still exists — it is not local to
test.

