This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes javascript var Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "javascript var" Watch "javascript var" New topic
Author

javascript var

Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

Place the following script fragment into an html page and display it.


What do you conclude from the results?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

BONK!
Andy Hahn
Ranch Hand

Joined: Aug 31, 2004
Posts: 225
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 60785
    
  65

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javascript var
 
Similar Threads
Control a String Value Display
"Unterminated <c:out tag" Error
passing the value of a parameter from java to javascript
custom properties in Input - bad in Netscape?
parseInt method