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>");
file: /Techref/language/java/script/definitive/ch05_10.htm, 5KB, , updated: 2019/10/14 15:00, local time: 2024/11/24 14:55,
3.14.250.187:LOG IN
|
©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? <A HREF="http://sxlist.com/TECHREF/language/java/script/definitive/ch05_10.htm"> [Chapter 5] 5.10 var</A> |
Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
Welcome to sxlist.com! |
.