1

I was trying out some simple JS code. I was aware that we should use var keyword to declare a loop variable inside the loop say for loop to avoid global variable declaration. However I realized that the loop variable exists after the execution of the loop too:

var a = [1, 2, 3, 4, 5, 6];
for (var i = 0; i < a.length; i++)
    document.write(a[i]); //123456
document.write(i);  //6

This is not inline (in fact it does not need to be, I know) with how loop variable of for loop in Object Oriented concepts behaves. I know I should not try to compare the JavaScript with in OO language for any aspect. However I am just guessing that the behavior in OO languages is better.

I think, if we put this code in directly in <script> tag then it actually adds the loop variable to global scope which is not desired. Is it like that? or I am thinking it wrong way?

We can achieve such behavior by creating explicit function scope:

var a1 = [1, 2, 3, 4, 5, 6];
(function () {
    for (var i1 = 0; i1 < a.length; i1++)
        document.write(a[i1]); //123456
})();

document.write(i1);  //undefined

But is it the standard way / followed in production? What other things can be done?

Mahesha999
  • 22,693
  • 29
  • 116
  • 189

3 Answers3

2

We can achieve such behavior by creating explicit function scope. But is it the standard way / followed in production?

Yes. Functions are the only possibility to introduce a new scope in JavaScript (though that might change in future versions with let); and IIFEs are heavily used in production as well.

What other things can be done?

To limit the scope of i to the for-loop's block? None (let alone let). However, you hardly will need to do that, since nothing will interfere with it - var i is still scoped local to your function. Sometimes, you even might want to use i after the loop as an ongoing counter.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • But does that means every `for` need to be nested inside function scope? – Mahesha999 Apr 14 '13 at 13:34
  • No. Only if you need `i` to be scoped explicitly on the loop, but that's hardly the case. – Bergi Apr 14 '13 at 13:39
  • 1
    rhetoric? may be yes, I tried to frame it neatly, but not just to cause huge responses and hits. Else every good question will be a rhetoric. I usually find related important stuff about the old topics I read while going through the web, far after initial reading. So just want to check if I missed anything. It is very possible with language like JS which is so flexible and has separate books dedicated just to patterns followed. The answer to the question can be No, there is no other known way or pattern just to address this issue, but still this will add some value to my understanding. – Mahesha999 Apr 14 '13 at 13:43
  • Maybe I did not understand what *other things* exactly you were asking for. Do you know closures already (the next thing related to function scope)? If not, [read on them](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – Bergi Apr 14 '13 at 13:51
0

JavaScript has only function scope any variable defined using var in side function is available inside function only. for loop does not create any scope for variables.

Anoop
  • 23,044
  • 10
  • 62
  • 76
  • yes that I know, but many patterns, practices evolve to take advantages of OOP in JS or even to say to make JS behave like OO language. Just want to know if any standard/well-followed workaround/pattern is there to scope loop variable to loop block. – Mahesha999 Apr 14 '13 at 13:29
0

Javascript is a bit strange and has, IMHO, a lot of defects that make it a poor language for middle/big projects.

"var" mean that the variable is only available in the current function scope: All variables are defined at the begening, before the function is run and set to "undefined". So:

alert(i);
var i=4;
alert(i);
var i=5;
alert(i);

internally mean:

var i=undefined;
alert(i);
i=4;
alert(i);
i=5;
alert(i);

In Structured programming, all code should be in functions, so that mean that your "var" keyword will work as expected. But if you write code in the global scope, "var" has no real sense.

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85