Its not wrong to declare a variable several times. For instance there is really no problem with: var I = 0; var I = 1 That's valid JavaScript. Good tools like the Closure Compiler will generate a warning though because you typically don't intend to do that That being said, even the Closure Compiler won't generate a warning for your example #2.It's just common convention in JS even if you are technically re-declaring Either of your two examples is fine but the second one is a little more sensible to parse.
I wouldn't worry about it either way.
Its not wrong to declare a variable several times. For instance there is really no problem with: var I = 0; var I = 1; That's valid JavaScript. Good tools like the Closure Compiler will generate a warning though because you typically don't intend to do that.
That being said, even the Closure Compiler won't generate a warning for your example #2. It's just common convention in JS even if you are technically re-declaring. Either of your two examples is fine but the second one is a little more sensible to parse.
I wouldn't worry about it either way.
Thanks for the answer. Isn't it however bad practice to declare the same variable multiple times in the same scope? Vars and functions are first-class citizens in javascript, which could make the preparation of a scope slightly slower, could it not?
– Witiko Oct 11 at 19:24 well, the compiler will actually only declare it once at the start of the function, so it won't be any slower at runtime. Look up "JavaScript hoisting" for why that is. – Simon Sarris Oct 11 at 19:26.
You don't want to be using the var keyword, but rather function arguments, because javascript is not block-scoped. For example: 100,200,300. ForEach(function (x,i) { 10,20,30.
ForEach(function (y,j) { console. Log('loop variables, indices '+i,j+' have values: '+x,y); }); }) or 100,200,300. Map(function (x,i) { return 10,20,30. Map(function (y,j) { return x+y; }); }) // result: 110,120,130,210,220,230,310,320,330.
Right, as I said it is a messy business, which makes ECMA5 methods a bliss. However, that doesn't really answer my question. :-) – Witiko Oct 11 at 19:13 @Witiko: Ah, oops, a developer who actually knows things.
=) Sorry, I thought you were merely asking "how do you make a nested for-loop work? " not an in-depth syntactic question. Hopefully someone else can shed light, or you can look at ecma-international.Org/publications/standards/Ecma-262.
Htm Good luck. (Might as well leave answer here for reference for others.) – ninjagecko Oct 11 at 19:29.
Those are both valid. Function scoped vs block scoped. Basically both loops in JavaScript become: function a () { var i, j; for(i = 0, j; I ; } } because the var declarations are hoisted to the top.
Well, a = 1; executed in the global scope will have the same effect as var a = 1; var a = 1; var a = 1;. That, however, doesn't make either of them a valid variable definition, which is what my question has been about. – Witiko Oct 11 at 19:17 I told you they are both valid friend, but I tried to explain why they are both valid due to hoisting and scope in JavaScript – IAbstractDownvoteFactory Oct 11 at 19:20 Right, thanks for the details provided I can see your point.
:-) – Witiko Oct 11 at 19:27.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.