Why does `eval` work on non-strings?
I was playing around with eval
and noticed that it can evaluate non-line strings in Chrome, Firefox and Opera:
eval(Array) === Array; // true
eval(this) === this; // true
eval(4 * 3 / 2) === 6; // true
Is this standard behavior? Is this documented anywhere? I can't seem to find any mention of eval
taking anything other than a string argument.
If this is not standard behavior, can someone identify host environments where this is not working?
source to share
Without the line, the code is already being evaluated at a lower level, namely before it is passed to eval (for example, your last statement is just executing eval(6)
). This is the case for any function; this is how JavaScript code is evaluated. eval
is not magic in this sense, because it is "just" a function that "just" takes an argument.
What eval
should be returned when an am expression that is not a string is passed is described in the spec :
1. If Type (
x
) is not a string, returnx
.
source to share