How to execute Javascript code ? Ie How to eval ?

About

eval evaluates a javascript expression (ie a javascript string) and therefore can eval a script

Example

eval("console.log('hello foo')");

Management

Scope

Most functions have access to the scope where they are defined, and nothing else. But eval has access to the full scope at the point where it’s called.

Strict

eval(..) when used in a strict-mode program operates in its own lexical scope.

function foo(str) {
   "use strict";
   eval( str );
   try {
       console.log( a ); // ReferenceError: a is not defined
   } catch (e) {
       console.log(e.message);
   }
}

foo( "var a = 2" );

Variable Binding

  • ''eval' will dynamically bind variable declared in the expression in the same execution scope
var y = "global";
function test(src) {
    eval(src); // may dynamically bind
    return y;
}
console.log(test("var y = 'local';")); // "local"
console.log(test("var z = 'local';")); // "global"
  • to avoid it, the scope must be restraint by using an iife
var y = "global";
function test(src) {
    (function() { eval(src) })(src); // may dynamically bind
    return y;
}
console.log(test("var y = 'local';")); // "global"
console.log(test("var z = 'local';")); // "global"

1)





Discover More
Browser
Browser - Javascript

javascript script in the browser Javascript code can be executed via the script html tag via the eval function that evaluates a string as javascript ...
How to load a script dynamically with Javascript?

This page shows you how you can load a script dynamically with Javascript to load and use it when needed. In short, if you add: a script via an DOM element, it will be executed (via append or insertAdjacentElement)...
Browser
How to use the setTimeOut javascript function (in the Browser)?

setTimeOut is a web api (browser function) that wraps code in a job and executes it later. The execution of the main thread (of the code) does not stop (not blocking). In this example, we will print...
Javascript Lexical Environment Scope Chaine
Javascript - (Variable) Scope (Namespace)

Variable scope in Javascript. variable scope is delimited by the function definition, not a the block level A block has the scope of its inner function. As a function is also an object, by generalization,...
Javascript - Apply

apply permits to call a function stored in a variable specifying: its execution context: this and its arguments eval A function that we will call with apply that: print the name property of...
Javascript - Expression

Javascript language expression: An expression produces a value whereas a statement will not. And therefore, expressions can be: passed where a variable is expected such as as function argument, assignment...
Card Puncher Data Processing
WeBlog - Dynamic Java Code Execution

Running java code stored in a string variable created dynamically inside a java application (ie at runtime) isnot straightforward in Java. This article: discuss a business case. explains why it's...



Share this page:
Follow us:
Task Runner