Subj : Re: "x = " vs. "var x = " To : netscape.public.mozilla.jseng From : Lau Lei Cheong Date : Thu Sep 09 2004 11:41 am It's clear now. Thanks for your reply. :D "Brendan Eich" ??? news:413DDB77.4010300@meer.net ???... > Lau Lei Cheong wrote: > > I also have this kind of problem, but for most time I simply take away the > > "var" and then ignore it. This seems a good chance to make it clear. > > > > Would you tell my why "var x=5" won't do it? To my understanding "initialize > > an variable with something" is the same as "declaring the variable, then > > assign its value with something". > > > Because of eval. eval("for (var i=0; i<0; i++);") should have no eval > result, meaning that eval returns undefined given that input, not false > or 0. The same goes for eval("var x=0"). > > Note the difference between no eval result and undefined eval result: > > eval("42; var x=43; 44") => 44 > > while > > eval("42; var x=43") => 42. > > JS is a C-like language, so it has statements as well as expressions, > and an expression can be a statement if started where a statement may > start and followed by a semicolon (or a newline, with automatic > semicolon insertion). The expression statements have result values when > eval'ed. Other statements and their contained expression-parts do not > set eval results. > > In the case of a var statement, the decision may seem arbitrary, since > one could cause any var contained in a for loop header to have no eval > result, but still allow var statements to have eval result. > > But in the interest of distinguishing declarations from expression > statements, the ECMA spec makes the former have no eval result. This > may help to future-proof the language design; it also allows certain > optimizations. > > /be > > > > > > Thank a lot. :) > > > > "Georg Maaß" ??? news:2q3rudFqq0fpU2@uni-berlin.de ???... > > > >>Sterling Bates wrote: > >> > >>>Just a quick q: why does the expression "var x = 5" return "undefined"? > >> > >>This is not an expression. This is a statement, declaring a variable x > >>initializing it with 5. > >> > >> > >>> The expression "x = 5" returns 5, as I would expect. > >> > >>This is an expression, not an initialization. > >> > >> > >>-- > >>Georg Maaß - bioshop.de D-76227 Karlsruhe, Westmarkstraße 82 > >>HTML, XML / JavaScript, C++, Java, PHP, VB / CGI, JSP, ASP, ASP.net > >> - The ultimate DHTML engine: http://gml-modul.sourceforge.net - > >> http://sourceforge.net/projects/gml-modul > >> > > > > > > .