Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javascript var

 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't find the answer to this question anywhere!

Can someone tell me when it is applicable to use the "var" keyword and when is it not? Or does the scope of the variable make a difference?

Is var supposed to be used for Strings only? What about Integers, Dates, etc?

Most of the advanced javascript functions I have been reviewing do not use var anywhere.

Thanks!!
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run this code:


I do this code when I do my talks. See if you can see what happens when you do not use var.

Eric
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to see how to code JavaScript better read an exerpt from my book: http://java.sun.com/javascript/ajaxinaction/Ajax_in_Action_ApB.html

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Place the following script fragment into an html page and display it.


What do you conclude from the results?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BONK!
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the keyword "var" declares the variable, and if I declare the variable at the global level and at the function level, they will be treated as two separate refrences? Is that correct?

So is it true that I do not have to use the "var" keyword if I want to declare a variable only at the global level?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Delcare your variables and use semicolons. If you don't, it leads to sloppy coding and that leads to problems.

Eric
[ June 15, 2006: Message edited by: Eric Pascarello ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64835
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Andy Hahn:

So is it true that I do not have to use the "var" keyword if I want to declare a variable only at the global level?


Essentially, but it is considered poor practice.

At global level, whether you use the var keyword to declare a variable, or cause it to be automatically created by assigning a value to a previously undeclared variable, it still ends up as a property on the window object.

Not declaring the variable is sloppy and I'd avoid it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic