Is it important to use var in the first bit of a for loop?


#1

Did it important or didn't?
Because why 'var' should or shouldn't be there?

var names = ['Marko', 'Nikola', 'Goran', 'Darko', 'Mladen'];
for (var i = 0; i < names.length; i ++)  {
    console.log("I know someone called " + names[i])
};

var names = ['Marko', 'Nikola', 'Goran', 'Darko', 'Mladen'];
for ( i = 0; i < names.length; i ++)  {
    console.log("I know someone called " + names[i])
};

#2

@medjed_mato I do not understand your issue, may you tell me your error message please?


#3

@claradavies Didn't errors. Both codes work fine!
I saw in 'for' loop in different cases, before 'i' (in first bit of 'for' loop) 'var' should be and should not be there.
I wonder why that is?
What are the advantages and disadvantages in such an coding?


#4

Well what the var does is it declares i as a variable to the computer, registering it in the computer's database. @medjed_mato


#5

Thanks @claradavies
Sure. Clear as day.
Do you always declares variable in 'for' loop and how would you write the code for any 'for' loop?


#6

In a for loop I would make it like this
for(var i = 0; i < 11; i++)
How the computer reads this is the variable i is equal to 0, while i is less than 11 increase it by one. What this would do is increase i by one until i is not less than 11, when i is 11


#7

With var you request space somewhere that can be accessed by the name that comes after it. Also this step, called variable declaration, makes sure that this space is in a certain scope. As you might remember from the exercises in Unit 3 each function can have it's own set of variables so if you declare a variable inside of a function it is only available there whereas if you declare it outside of the function (on the same level as the variable that holds the function) it is also available inside the function.

Why is this important?
And why does it work as well without using the var?
And is it recommended/necessary to use the var?

Well, what happens when you just assign a value to a variable is that you implicitly declare it to be a property of the window object. Ok, you probably don't know yet what objects and properties of objects are but what that means in this case is that it is global for the whole window. I've run a test and if you use arbitraryName = value on one exercise you can still access this value when you switch to the next exercise. So a variable of an exercise rather long ago (unless you refresh the page which resets the variables) could do harm to your code now and the more global variables you're using the bigger the complexity becomes and the more likely you get errors from it. And these are some kind of errors that are obviously hard to track because they might have nothing to do with your exercise.

So as said with var you make sure that a variable is tied to a certain scope and even this just means that it belongs to one exercise. I hope this is enough input on why it is useful to use var. If you still have questions feel free to ask.

PS: I changed you're title hope you like it, if not just change it again.


#8

Thank you very much @haxor789
Well explained. I think that I should more often consulted with you.
Thanks others peoples, they all had contributed well. (@claradavies )

PS: Sure, sure, changed fine. Anyway my english isn't enough strong. Thanks again. :wink: