aspose file tools
The moose likes HTML, CSS and JavaScript and the fly likes javascript var Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply 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: 15357
    
    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: 15357
    
    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: 56224
    
  13

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


What do you conclude from the results?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56224
    
  13

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: 15357
    
    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: 56224
    
  13

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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: javascript var
 
Similar Threads
passing the value of a parameter from java to javascript
"Unterminated <c:out tag" Error
Control a String Value Display
parseInt method
custom properties in Input - bad in Netscape?