please dont rip this site

JavaScript: The Definitive Guide

Previous Chapter 5
Statements
Next
 

5.10 var

We saw the var statement in Chapter 3, Variables and Data Types; it provides a way to explicitly declare a variable or variables. The syntax of this statement is:

var name_1 [ =index.html value_1] [ ..., name_n [=index.html value_n]]

That is: the var keyword is followed by a variable name and an optional initial value, or it is followed by a comma-separated list of variable names, each of which can have an initial value specified. The initial values are specified with the = operator and an arbitrary expression. For example:

var i;
var j =index.html 0;
var x = 2.34, y = 4.12, r, theta;

If no initial value is specified for a variable with the var statement, then the variable will be defined, but its initial value will be the special JavaScript undefined value.

The var statement should always be used when declaring local variables within functions. Otherwise, you run the risk of overwriting a top-level variable of the same name. For top-level variables, the var statement is not required. Nevertheless, it is a good programming practice to use the var statement whenever you create a new variable. It is also a good practice to group your variable declarations together at the top of the program or at the top of a function.

Note that the var statement can also legally appear as part of the for and for/in loops, in order to declare the loop variable as part of the loop itself. For example:

for(var i =index.html 0; i < 10; i++) document.write(i, "<BR>");
for(var i = 0, j=10; i < 10; i++,j--) document.write(i*j, "<BR>");
for(var i in o) document.write(i, "<BR>");
A variable declared in a loop in this way is not local to the loop as it would be in C++ or Java; its scope is the same as it would be if it had been declared outside of the loop.


Previous Home Next
with Book Index function

HTML: The Definitive Guide CGI Programming JavaScript: The Definitive Guide Programming Perl WebMaster in a Nutshell

file: /Techref/language/java/script/definitive/ch05_10.htm, 5KB, , updated: 2019/10/14 15:00, local time: 2024/11/8 10:56,
TOP NEW HELP FIND: 
18.217.122.161:LOG IN
©2024 PLEASE DON'T RIP! THIS SITE CLOSES OCT 28, 2024 SO LONG AND THANKS FOR ALL THE FISH!

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://linistepper.com/Techref/language/java/script/definitive/ch05_10.htm"> [Chapter 5] 5.10 var</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?