"x = " vs. "var x = "

Just a quick q: why does the expression "var x = 5" return "undefined"? 
  The expression "x = 5" returns 5, as I would expect.

Inside jsshell "var x = 5" doesn't print a value (I assume it's 
suppressing undefined results).

Sterling
0
Sterling
9/6/2004 5:11:09 PM
netscape.mozilla.jseng 1530 articles. 0 followers. Follow

5 Replies
920 Views

Similar Articles

[PageSpeed] 52
Get it on Google Play
Get it on Apple App Store

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

0
ISO
9/6/2004 7:27:58 PM
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".

Thank a lot. :)

"Georg Maa�" <georg@bioshop.de> ??? 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
>


0
Lau
9/7/2004 6:34:15 AM
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�" <georg@bioshop.de> ??? 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
>>
> 
> 
> 
0
Brendan
9/7/2004 4:01:59 PM
It's clear now. Thanks for your reply. :D

"Brendan Eich" <brendan@meer.net> ??? 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�" <georg@bioshop.de> ??? 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
> >>
> >
> >
> >


0
Lau
9/9/2004 2:41:12 AM
Brendan Eich wrote:
> 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.

and

eval("42; void(x=43);") => void, i.e. undefined

void(x=43) is an expression representing a value which is undefined and 
therefore this causes the result of eval to be exactly that.
-- 
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
0
ISO
10/9/2004 12:50:34 PM
Reply:

Similar Artilces:

Precedence of "where" ("of", "is", "will")?
Nobody on #perl6 today could answer this one. Is: Str | Int where { $_ } the same as: (Str | Int) where { $_ } or: Str | (Int where { $_ }) ? Followup questions, Mr. President: What kind of operators are "where", "of", "is", and "will"? Is there a reason that S03 doesn't list them? What are their precedence(s)? -- Chip Salzenberg - a.k.a. - <chip@pobox.com> Open Source is not an excuse to write fun code then leave the actual work to others. Chip Salzenberg writes: &...

.ALLCOL("%COLUMN%", " ", ", ", ", ")
Do you know anyway for me to exclude a subset of columns returned by this function. We have two columns (rec_user and rec_datetime) which are in all of our tables, but when generating triggers I want automatically generate a script which does not include those two columns but does include all other columns in that table. Bruce I should add that I am using PD 9.0.0.580. Bruce "Bruce Lamb" <lamb.bruce@mayo.edu> wrote in message news:6HgI315nCHA.155@forums.sybase.com... > Do you know anyway for me to exclude a subset of columns returned by this > function. ...

$x ="('what is happening here?')"; print "${$x}";
This is a bug report for perl from dmi@questrel.com, generated with the help of perlbug 1.28 running under perl v5.6.0. ----------------------------------------------------------------- [Please enter your report here] $x ="('what is happening here?')"; print "${$x}"; [Please do not change anything below this line] ----------------------------------------------------------------- --- Flags: category=core severity=low --- Site configuration information for perl v5.6.0: Configured by dmi at Sat Jun 3 20:07:43 PDT 2000. Summary of...

RFE: for "%%" becoming to "&&" as "//" is to "||"; the "defined(x) && expr" operator...
In looking at the 5.10 perl5op doc's, I was thinking about the "//" operator and how in an expression "a // b" it tests 'definedness' the left-hand-side and returns it if it is defined, otherwise, it returns the right hand side. The idea being that you might normally use "||" but if the 'lhs' could be integer '0', it wouldn't work as desired. I think it could be useful to provide an equivalent operator for '&&', say '%%', that tests 'defined(lhs)' and if true, returns the 2nd part (rig...

quotes, quotes, quotes...
I am getting this error and I know what is causing it, but I have no idea how to fix it, any help would be great. The script steps through the /var/log/messages file on a linux server and puts The entries into a mysql database. However when it gets to the 'hlt' line in the messages file it just barfs. The single quotes are freaking it out. I know about quotes but not how to use in this situation. Thanks, Paul Error: May 27 17:53:00 localhost kernel: Checking 'hlt' instruction... OK. <----- doesn't like this in the messages file DBD::mysql::st exec...

"PopupExtender" vs "PopupControl" vs "PopupExtenderControl"
I am looking through the Toolkit source code and trying to understand it wrt the PopupExtenderControl.  In the project I see two folders: "PopupExtender" and "PopupControl".  These contains classes "PopupControlExtender" and "PopupExtender" and these both eventually derived from "AnimationExtenderControlBase".Do these two classes work in conjunction to implement the PopupExtender?  Or are they separate, autonomous extenders wth different purposes?  If so, which one corresponds to the PopupExtender on the sample page? ...

$x ="('what is happening here?')"; print "${$x}"; #2
Richard Foley <Richard.Foley@m.dasa.de> claimed to have written: > $x ="('what is happening here?')"; > print "${$x}"; What indeed *is* happening here? It's bad enough that all these reposted bug reports have apparent source <Richard.Foley@m.dasa.de>, since it's generally impossible to reply to the original poster without rummaging in the bug database to find the real message details. But this one doesn't even have the bug ID (which should be 20000607.009). What gives? (I also notice that in the bug database, the bug ...

"Using" or "With"
Hi all Please can someone enlighten to me as regards the difference with the "Using" and "With" statement when accessing data - which is better, what are the limitations and/or any pointers. Many thanks. Regards DaveDavid WinchesterPlease mark as answer if this is the solution.  using gives you the ability to use the connection and it closes the connection directlly after you finish using it. and there is no need to try- cach - finaly. there is no limitation on using USING keywordMuhanad YOUNISMCSD.NETMy Blog || My Photos || LinkedIn I have a dataobject the re...

"To" and "From" missing
When I print emails, the words "To" and "From" are blank, even though the "To" name and "From name (addresser, addressee) do show up. This is not a problem for other users on my system. Suggestions In mailbox right click, view. On the message window, right click and choose print options. Make sure print header is checked. -- Barry Merchant NSC Volunteer SysOp *** no email unless requested please!! *** > In mailbox right click, view. On the message window, right click and > choose print options. Make sure prin...

"Mozilla Sunbird" vs. "Calendar"
Hi there ! Older version used to install in a directory named "Calendar". Somewhere in the 0.7 line, this changed to "Mozilla Sunbird", which sounds reasonable according to FF and TB. And as the update is still broken, I installed 0.8pre manually and ended up with "Calendar" again. Is this a bug or is it maybe just a difference between "pre" and "final"? I'm not sure when the change happened exactly. Regards fw Frank Winkler wrote: > Is this a bug or is it maybe just a difference between "pre" and > ...

double quote
hello there...  i tried everything of think but not working the way i wanted to be... not sure what i'm missing...i'm generating a <span> in code behind and then using in javascript.... here is what i'm doing code behind: int i=0string _keywordID = "keyword";string _name = row["visit_info_nm"].ToString().Trim(); String _getElementByID = String.Format("<span id='{0}' OnClick = \"document.getElementById('{1}').value='{2}';\">{3}</span><br>", i, _keywordID, _name, _name); here is what it generate : <span id='1' OnClick = \"document.getElementById('keyword')...

"-" not "_"
I wrote a SQL statement in the data tab. I wrote a bunch of alaises as example ' word-type ' but when I hit the layout tab it converts the "-" to "_". So now my field name is ' word_type '. Is there any way to prevent this? CardGunner Don' use a hypen ( - ).  It isn't a valid character for column names.   See http://searchsqlserver.techtarget.com/expert/KnowledgebaseAnswer/0,289625,sid87_gci1188931,00.html   Here's an excerpt about column names: Letters as defined in the Unicode Standard 2.0 Decimal numbers from either B...

"Me" is better than "You"
Yes I know, strings are frozen. But let me talk about it, I really can't get through the idea of a PC talkin to me. I consider my PC as an extension of myself, not a dumb companion who addresses Me as You. Yes there are times when I get angry with Him while I work and get wrong calculations etc.., but it really is my fault, Me using wrong istructions and eventually wanting to find someone else to blame, but it's Me. And yes, I consider Thunderbird my mail program, reading my mail on my PC as Me. So I personally like to have Me in the header bar as a compact address ...

Using "+" or "||"
Using SQLAnywhere 5.5.04, I've gotten into the habit of using "||" in ISQL to indicate a string concatenation. I needed to paste my SQL statement into the PowerBuilder script painter for some embedded SQL, and PB didn't like the "||" very much at all. I changed it to "+" and it seems to be ok. Do these two operators indicate ~exactly~ the same thing? moin, afaik these two's are not the same! if you're using "||" and any term is NULL then in the resultstring the term will be ignored if you use "+" then the resu...

Web resources about - "x = " vs. "var x = " - netscape.mozilla.jseng

Resources last updated: 12/20/2015 11:51:57 PM