Javascript - eval


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


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



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.


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) {

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"


Discover More
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)...
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...
What is the script HTML element?

A client-side script is a program that is: linked (server-side script) or directly embedded in an HTML document (in-line script) Scripts in HTML have “run-to-completion” semantics, meaning that...

Share this page:
Follow us:
Task Runner