Difference between window[] and eval() - Javascript

Related searches

I've been using both in javascript ... really don't know the difference. Googling always shows results for the "window object" or "opening a new window in javascript" so couldn't find anything there.


Sometimes window works for me and at other times eval works ....

So what's the difference between eval() and window[] ?

Sorry for the newbie question though !


Another point that has not been addressed is that eval will resolve the variable reference using the caller variable environment, for example:

var foo = "global";

(function () {
  var foo = "local";
  alert(eval("foo")); // alerts "local"
  alert(window["foo"]); // alerts "global"

So as you can see, is not completely equivalent.

If you simply want to reference a global variable, I would recommend you to use the window[prop] approach and avoid surprises.

Difference between eval() and window[], Difference Between eval() and window[]. Both eval and window[] is used to convert string to JavaScript variable or method but little difference is shown below . JavaScript: window, document and screen Window. The JavaScript window object sits at the top of the JavaScript Object hierarchy and represents the browser window. The window object is supported by all browsers. All global JavaScript objects, functions, and variables

eval() interprets arbitrary javascript statements, whereas with window you are accessing a property of the window object.

In your example, you seem to be using a property name in both eval() and window[]. As the global scope in a browser is the same as the window object's scope they will evaluate to the same thing.

You can think of your eval("v"+e) statement as being equivalent to eval("window['v'" + e +" ]").

eval(), If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements. The eval() function evaluates or executes an argument. If the argument is an expression, eval() evaluates the expression. If the argument is one or more JavaScript statements, eval() executes the statements.

Both return a global variable's value. The difference is that if the global variable is undefined, you will get an error on executing eval() whereas window['variableName'] will return undefined(not an error) because accessing an undefined property is not an error but accessing an undefined variable is an error.

Global eval. What are the options? — Perfection Kills, eval(). eval(str) evaluates the JavaScript code in str. For example: > Indirectly: in some other way (via call(), as a method of window, by storing it� Without use strict, eval doesn’t have its own lexical environment, so we would see x and f outside. Using “eval” In modern programming eval is used very sparingly. It’s often said that “eval is evil”. The reason is simple: long, long time ago JavaScript was a much weaker language, many things could only be done with eval. But that

"v"+e -> string

eval(x) -> evaluates the string x, containing javascript expression

window[x] -> returns window's property with the same name, as tha value of x is. this in fact can be a global variable

therefore, when you have a global variable v1 = "foo", and e = 1, then eval("v"+e) and window["v" + e] both return "foo"

JavaScript eval() Function, Both JavaScript vs JScript are designed to make dynamic web pages and interactive content. JScript was the first release in 1996 being supported by Internet Explorer 3.0. The current version of Internet Explorer 9 supports JScript 9.0, and that’s the most recent version.

Evaluating JavaScript code via eval() and new Function(), Windows 8 Enterprise is for business use and does not expire. Windows 8 Enterprise Evaluation is a trial of 90 Days and will expire.

  • Could you state that as a question, please? Specifically, what are you really asking?
  • Whatever the answer, don't use eval!
  • I think it's pretty clear that the question is "please explain the situation to me".
  • @ jasongetsdown - small question ... why shouldn't eval be used ? Is there a better way to access local variables like so var c = eval("ev"+d); where d=1 locally (this value is passed to a function) and I want to set the value of c equal to the value of local varable ev1 ?
  • @Norman: the answer to that is, if you're new and using eval, you're probably doing something wrong. Eval does have a purpose, but newcomers probably aren't using it for that. (this is an old question that was recently brought up, so this question is meant for others)
  • So in order to access local variables use window otherwise use eval ? ... why do so many people discourage the use of eval? is there another way to access local variables like so eval("ev"+d); where d=1 locally and I want to access the value of local varable ev1 ?
  • @Norman, to access global variables use window[prop], see this question to know why you shouldn't use eval when you don't need it..
  • Well put. But to make sure its clear - eval is the bad way to do it; the other is the good way to do it.
  • Yes, eval is almost always a bad idea.