• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javascript var

 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BONK!
 
Andy Hahn
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic