Forum : How to use the same variable name several times in a script?
Brief description  about Online courses   join in Online courses
View Abhijit  Paul 's Profile

How to use the same variable name several times in a script?

Hi,
Can any one suggest me how to use the variable name several times in a script..
Asked by Abhijit Paul | Nov 23, 2009 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Abhijit Paul,

By default all variables in JavaScript are global, meaning they retain their values

everywhere in a scripted web page. To make a variable local to a block (such as a

function), declare it with the keyword var. Here's a n example:

// here 'i' is a global counter variable that could, if reused
// elsewhere in this script, create hard-to-find errors

for( i=1; i<=N; i++ ) {
[code to iterate]
}

// to ensure that 'i' exists only in the scope of this
// counter, use var:

for( var i=1; i<=N; i++ ) {
[ ... ]
}

hope you got your answer..
Nov 23, 2009