In Javascript, eval
behaves in four different ways, depending on how you call it. This is illustrated by the following piece of code:
var myEval = eval;
var x = "GLOBAL_X";
var y = "GLOBAL_Y";
function testEval(useStrict, direct) {
var x = "local_x";
var evalStr = "var y = 'inside_y'; x;";
evalStr = (useStrict ? "'use strict';\n" : "") + evalStr;
console.log(direct ? eval(evalStr) : myEval(evalStr), " ", y);
}
testEval(true, true);
testEval(false, true);
testEval(true, false);
testEval(false, false);
The standards-complient output (assuming console.log is defined) is,
local_x GLOBAL_Y
local_x inside_y
GLOBAL_X GLOBAL_Y
GLOBAL_X inside_y
You’ll need an up-to-date browser to see the “correct” behavior.
Just discovered this gem. It’s common knowledge that NaN != NaN. But did you know there are values x, y, and z such that x < y, y < z, and z < x?
var x = "2";
var y = 3;
var z = "021";
console.log(x < y, y < z, z < x); // Prints true true true