View Full Version : Resolved Local variables in a for() loop?

01-07-2009, 09:23 PM
Hi all,

What is the best way to declare a variable within a for() loop so that it is not accessible from outside of the loop?


01-07-2009, 11:03 PM
It isn't accessible from outside of the loop... If you have a global variable name and you use the same name in a "for" loop, it won't work cuz it's illegal. I don't quite understand how your question would be executed (considering your asking if something possible, and it already is...). Any clarifications, or did you mean exactly that? Also, could you post code that you are using so that we can give a firm solution? In closing, you should always create a for loop variable the same way you do with others: for(var i = 0; i < 10; ++i); (etc.)

var may make 'i' global- I don't know...


01-07-2009, 11:23 PM
Magic - you can access it from the outside:

<script type="text/javascript">
var end = i;

The end variable is declared inside the for, and can be accessed outside the end.
I've just realized what magicyte meant, you were talking about the i=0, I'm talking about a variable in the { and }. Can you please specify which variables? Mine or magics.

01-07-2009, 11:48 PM
Oh- in that case, you COULD make it private SOMEHOW. I may have forgotten, though.


01-07-2009, 11:51 PM
No - that will still be global.

01-07-2009, 11:56 PM

Anyway, why would you not want your variable being declared inside of the for() loop accessed outside? You wouldn't NEED to access it unless you had the same variable name in a global scenario... That should flag an error, though.


01-08-2009, 02:55 AM
To make a variable local, regardless of whether or not it is in a loop, it must be in its own function. If the loop is already in a function, simply formally declaring the variables used within the loop will make them local to that function.

If you want them local to the loop, and only to the loop, put the loop itself in a separate function, ex:

var preload_looper = function(ar){
for(var par = [], i = 0; i < ar.length; ++i){
par[i] = [ar[i], new Image()];
par[i][1].src = par[i][0];

The variables par, and i will be local to the function, and therefore the loop. And since nothing is done to the passed array (ar), it will not be changed by the function or the loop.

01-08-2009, 03:51 AM
Based on what John said above here is another example that uses an anonymous function through which it maintains a private variable.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<style type="text/css">
<script type="text/javascript">
for(var i = 0; i < 10; i++){
var k = i;
})(); //Self executing anonymous function
alert(typeof k); //You are trying to access the variable k here. This will give you 'undefined' as its value.

JavaScript doesn't allow block level variables whose scope is the block in which it declared.

Hope this helps.

01-08-2009, 03:48 PM
Oh- in that case, you COULD make it private SOMEHOW. I may have forgotten, but this is the way I think that it goes:
for(var i = 0; i < 10; ++i) {
yourVariable = "whatever";
}No, that will make it global, not even limited to the function scope as it would be with a var keyword (technically speaking, you're declaring a property of the global object rather than a variable).

Javascript doesn't, as magicyte has just (like many C programmers before him) found out, have block scope. The only way to create a new scope (at least until JS1.7 with its let blocks has better support) is with a separate function, as John and codeexploiter have demonstrated; however, there is one flaw in codeexploiter's model: on each iteration, the function will be created anew. That's quite nastily inefficient. Better would be to create a separate function elsewhere, passing in assorted variables where necessary. In terms of codeexploiter's loop:
var looper = (function() {
function helper(i) {
var k = i;

function looper() {
for (var i = 0; i < 10; ++i)

return looper;

01-08-2009, 04:23 PM
That's not merely local (private), it's downright exclusive. But it does fit the bill.

01-09-2009, 12:12 AM
Holy crap, thanks for the in-depth answers guys.