[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/eval [Back]  [Original]

eval() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

eval()

Baseline

20157

: JavaScript eval() eval() !

eval() JavaScript

console.log(eval("2 + 2"));
// : 4

console.log(eval(new String("2 + 2")));
// : 2 + 2

console.log(eval("2 + 2") === eval("4"));
// : true

console.log(eval("2 + 2") === eval(new String("2 + 2")));
// : false

js
eval(script)

script

JavaScript import

undefined script eval()

script SyntaxError

eval()

eval() let undefined

eval eval SyntaxError

js
"use strict";

const eval = 1; // SyntaxError: Unexpected eval or arguments in strict mode

eval() eval() String eval() String

js
eval(new String("2 + 2")); // "2 + 2"  String 
eval("2 + 2"); // 4 

eval()

js
const expression = new String("2 + 2");
eval(String(expression)); // 4 

eval

eval() eval eval 2 eval eval eval(...) ?.

js
// 
eval("x + y");

// eval 
(0, eval)("x + y");

// 
eval?.("x + y");

// eval 
const myEval = eval;
myEval("x + y");

// 
const obj = { eval };
obj.eval("x + y");

eval <script>

  • eval

    js
    function test() {
      const x = 2;
      const y = 4;
      // 
      console.log(eval("x + y")); //  6
      // 
      console.log(eval?.("x + y")); // x 
    }
    
  • eval "use strict"

    js
    function nonStrictContext() {
      eval?.(`with (Math) console.log(PI);`);
    }
    function strictContext() {
      "use strict";
      eval?.(`with (Math) console.log(PI);`);
    }
    function strictContextStrictEval() {
      "use strict";
      eval?.(`"use strict"; with (Math) console.log(PI);`);
    }
    nonStrictContext(); // Logs 3.141592653589793
    strictContext(); // Logs 3.141592653589793
    strictContextStrictEval(); // Uncaught SyntaxError: Strict mode code may not include a with statement
    

    eval

    js
    function nonStrictContext() {
      eval(`with (Math) console.log(PI);`);
    }
    function strictContext() {
      "use strict";
      eval(`with (Math) console.log(PI);`);
    }
    function strictContextStrictEval() {
      "use strict";
      eval(`"use strict"; with (Math) console.log(PI);`);
    }
    nonStrictContext(); // Logs 3.141592653589793
    strictContext(); // Uncaught SyntaxError: Strict mode code may not include a with statement
    strictContextStrictEval(); // Uncaught SyntaxError: Strict mode code may not include a with statement
    
  • var eval eval var

    js
    //  var 
    eval("var a = 1;");
    console.log(a); // 1
    //  eval  b 
    eval("'use strict'; var b = 1;");
    console.log(b); // ReferenceError: b is not defined
    
    function strictContext() {
      "use strict";
      // 
      eval?.("var c = 1;");
      //  eval  d 
      eval("var d = 1;");
    }
    strictContext();
    console.log(c); // 1
    console.log(d); // ReferenceError: d is not defined
    

    let const

  • eval new.target

    js
    function Ctor() {
      eval("console.log(new.target)");
    }
    new Ctor(); // [Function: Ctor]
    

eval() !

eval()

  • eval() eval() eval() eval
  • JavaScript JS eval()
  • JavaScript JavaScript eval() eval()
  • eval() eval()

eval()

eval()

js
function looseJsonParse(obj) {
  return eval(`(${obj})`);
}
console.log(looseJsonParse("{ a: 4 - 1, b: function () {}, c: new Map() }"));

eval

js
function looseJsonParse(obj) {
  return eval?.(`"use strict";(${obj})`);
}
console.log(looseJsonParse("{ a: 4 - 1, b: function () {}, c: new Map() }"));

2 eval

  • c: new Map() eval Map Map Map() eval Map window.Map()

    js
    function looseJsonParse(obj) {
      class Map {}
      return eval(`(${obj})`);
    }
    console.log(looseJsonParse(`{ a: 4 - 1, b: function () {}, c: new Map() }`));
    

    eval() Map()

  • eval() var

  • eval

  • eval eval eval()

eval() Function()

Function()

Function() eval JavaScript eval()

eval() Function()Function() return

Function() eval

js
function add(a, b) {
  return a + b;
}
function runCodeWithAddFunction(obj) {
  return Function("add", `"use strict";return (${obj});`)(add);
}
console.log(runCodeWithAddFunction("add(5, 7)")); // 12

eval() Function() CSP eval() Function()

eval() eval()

js
const obj = { a: 20, b: 30 };
const propName = getPropName(); // returns "a" or "b"

const result = eval(`obj.${propName}`);

eval() propName getPropName

js
const obj = { a: 20, b: 30 };
const propName = getPropName(); // "a"  "b" 
const result = obj[propName]; // obj[ "a" ]  obj.a 

eval()

js
const obj = { a: { b: { c: 0 } } };
const propPath = getPropPath(); // "a.b.c" 

const result = eval(`obj.${propPath}`); // 0

eval()

js
function getDescendantProp(obj, desc) {
  const arr = desc.split(".");
  while (arr.length) {
    obj = obj[arr.shift()];
  }
  return obj;
}

const obj = { a: { b: { c: 0 } } };
const propPath = getPropPath(); // "a.b.c" 
const result = getDescendantProp(obj, propPath);

js
function setDescendantProp(obj, desc, value) {
  const arr = desc.split(".");
  while (arr.length > 1) {
    obj = obj[arr.shift()];
  }
  return (obj[arr[0]] = value);
}

const obj = { a: { b: { c: 0 } } };
const propPath = getPropPath(); // "a.b.c" 
const result = setDescendantProp(obj, propPath, 1); // obj.a.b.c  1 

JavaScript API DOM API

js
// setTimeout(" ... ", 1000) 
setTimeout(() => {
  // 
}, 1000);

// elt.setAttribute("onclick", "") 
elt.addEventListener("click", () => {
  // 
});

JSON

eval() "[1, 2, 3]" JSON JavaScript

JSON JavaScript JavaScript JSON JSON JSON JSON

JavaScript XPath

eval()

eval() 42 "x + y + 1" 2 "42"

js
const x = 2;
const y = 39;
const z = "42";
eval("x + y + 1"); // 42
eval(z); // 42

eval() if

js
const str = "if (a) { 1 + 1 } else { 1 + 2 }";
let a = true;
let b = eval(str);

console.log(`b is: ${b}`); // b is: 2

a = false;
b = eval(str);

console.log(`b is: ${b}`); // b is: 3

eval() str x 5 z 42 z 0 JavaScript 2 eval() z

js
const x = 5;
const str = `if (x === 5) {
  console.log("z is 42");
  z = 42;
} else {
  z = 0;
}`;

console.log("z is ", eval(str)); // z is 42  z is 42

js
let x = 5;
const str = `if (x === 5) {
  console.log("z is 42");
  z = 42;
  x = 420;
} else {
  z = 0;
}`;

console.log("x is", eval(str)); // z is 42  x is 420

eval "(" ")"

js
// 
const fctStr1 = "function a() {}";
// 
const fctStr2 = "(function b() {})";
const fct1 = eval(fctStr1); // undefined  `a` 
const fct2 = eval(fctStr2); //  `b` 

ECMAScript 2027 LanguageSpecification
# sec-eval-x


Web Proxy Viewer  |  New URL  |  Original Page