Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Ravi Yadav
10,152 PointsWhat's the difference in performance of the following for loops?
Hi, I was going through the Q&A and went through a blog which said the optimized way of writing a for loop is to define the variable outside of for loop:
var x;
for ( x = 0; x < 10; x++)
{
\\your code goes here\\
}
My question is, when we define the variable inside for loop, eg. for ( var x=0; x<10; x++) Wouldn't it be the same thing ? My understanding is that the variable is defined only once even if we define the var x inside. So what difference does it make if any?
tobiaskrause
9,160 PointsForget what I said. JavaScript has no block scope just function scope. I did too much C# the last weeks :) But ES2015 will have block scope keep that in mind
Ravi Yadav
10,152 PointsThis is the example I was talking about. I didn't understand the explanation.
1 Answer
tobiaskrause
9,160 PointsWell your example is a for...in loop not a normal for loop, thats something different. In the for...in loop the variable gets new created in every loop. When you declare the varaibe before the for...in loop its value just gets changed.
When you use "var x in ... " in a for...in loop the variable gets new created for every loop. When you created it before the loop, only the value of the varable will change.
In a for loop the "var x" gets created once, and its value gets changed. It does not matter if you create it before the for loop or in the head of the loop of it. The declaration and initialization in normal for loops only happens once.
But if you want to know more about loops i would suggest you this
Ravi Yadav
10,152 PointsSo in this example ( normal for loop) it wont make a difference in performance after all as Zdenak Krcal pointed out. It all makes sense now. Your first answer had cleared my confusion though. It's all about scope that makes difference in performance(for..in example).
tobiaskrause
9,160 PointsWell yeah but be careful block scope does not exist in JS yet...only in ES2015...
for (var x = 0; x < 10; x++)
{
\\your code goes here\\
}
console.log(x); //this will work
You have STILL access to x if it is at the function level (part of your function) but you dont have access outside of your function. In other programming languages it is different.
Zdenek Krcal
49,910 PointsZdenek Krcal
49,910 PointsHi, I there is not difference. var is a directive for the parser, and not a command executed at run-time.