469,330 Members | 1,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Good practice question: Declaring/Initializing variables inside or outside a loop ?

SM
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.

What's a better practice?
Declaring and initializing variables inside a loop routine, like this:

for(var i=0; i<list; i++)
{
var name = list[i];
}
or outside a loop routine, like this:
var i;
var name;

for (i=0; i<list; i++)
{
name = list[i];
}

or are both the same....


As a programmer, i always try to practice good programming. I always
thought that by declaring and initializing the variable inside the
loop, i was creating a new memory space every time instead of just 1
time...

What do you think?

Thanks
Marco

Apr 30 '07 #1
8 7173
Pretty much the same thing. This looks neater

for(var i=0; i<list; i++) {

}
On Apr 30, 11:51 am, SM <servandomont...@gmail.comwrote:
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.

What's a better practice?
Declaring and initializing variables inside a loop routine, like this:

for(var i=0; i<list; i++)
{
var name = list[i];

}

or outside a loop routine, like this:

var i;
var name;

for (i=0; i<list; i++)
{
name = list[i];

}

or are both the same....

As a programmer, i always try to practice good programming. I always
thought that by declaring and initializing the variable inside the
loop, i was creating a new memory space every time instead of just 1
time...

What do you think?

Thanks
Marco

Apr 30 '07 #2
On Apr 29, 9:51 pm, SM <servandomont...@gmail.comwrote:
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.

What's a better practice?
Declaring and initializing variables inside a loop routine, like this:

for(var i=0; i<list; i++)
{
var name = list[i];

}
here you are declaring the "name" variable again and again .. which is
wrong

or outside a loop routine, like this:

var i;
var name;

for (i=0; i<list; i++)
{
name = list[i];

}
OR:
for(var i=0,name;i<list;i++){ name=list[i] }
----------^ : This expression is executed only once, before the loop
is executed
Apr 30 '07 #3
here you are declaring the "name" variable again and again ..

There's no chance to "declaring variable again and again" in
javascript. See 10.1.3 Variable Instantiation (ECMAScript Language
Specification).


Apr 30 '07 #4
I always thought that by declaring and initializing the variable inside the
loop, i was creating a new memory space every time instead of just 1
time...
Both of examples work equally.


Apr 30 '07 #5
On Apr 29, 9:51 pm, SM <servandomont...@gmail.comwrote:
I've always wonder if there is diference when declaring and
initializing a varible inside/outside a loop.
Performance-wise, no difference. All vars are processed before the
code executes.
What's a better practice?
It depends purely on how you want to organize your code.

It's potentially dangerous to not use 'var' in a loop variable because
you could reference a global variable. So, putting 'var' tightly
coupled to the loop is a good idea. It makes sure you aren't using a
global var, and if you copy and paste the code to another function
your var will go with it.

For variables with semantic meaning that hold values you want to do
something real with, it's not a bad idea to declare them at the top so
you know what you're going to work within inside the function. Also,
declaring them at the top with

var name,age,size;

rather than a 'var' before each use saves a few bytes of space ;)
As a programmer, i always try to practice good programming. I always
thought that by declaring and initializing the variable inside the
loop, i was creating a new memory space every time instead of just 1
time...
One of the disadvantages of using 'var' inside loops is that it gives
some people the impression that there is block scope in js. There is
not.

Matt Kruse

Apr 30 '07 #6
ak******@yandex.ru wrote:
>here you are declaring the "name" variable again and again ..

There's no chance to "declaring variable again and again" in
javascript. See 10.1.3 Variable Instantiation (ECMAScript Language
Specification).
I don't know exactly what "there's no chance to" means, but I believe
what scripts.contact meant was that in each iteration the variable
"name" is overwritten.

So unless name is used before the next iteration of the loop it is lost.
Hence it being "declared over and over again."

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 1 '07 #7
On May 1, 10:25 am, -Lost <maventheextrawo...@techie.comwrote:
aksus...@yandex.ru wrote:
here you are declaring the "name" variable again and again ..
There's no chance to "declaring variable again and again" in
javascript. See 10.1.3 Variable Instantiation (ECMAScript Language
Specification).

I don't know exactly what "there's no chance to" means, but I believe
what scripts.contact meant was that in each iteration the variable
"name" is overwritten.
If that is so, it is incorrect - declaring a variable a second time
within the same scope has no effect on its value. Read the reference
cited.

So unless name is used before the next iteration of the loop it is lost.
That makes no sense at all - declared variables are instantiated
before execution begins. Whether a particular iteration of a loop
assigns a value or not makes no difference to the variable's existence
(the case is quite different for undeclared variables though).

Hence it being "declared over and over again."
The declaration statement might be iterated over multiple times, but
the variable is effectively declared once and once only.
--
Rob

May 1 '07 #8
RobG wrote:
On May 1, 10:25 am, -Lost <maventheextrawo...@techie.comwrote:
>aksus...@yandex.ru wrote:
>>>here you are declaring the "name" variable again and again ..
There's no chance to "declaring variable again and again" in
javascript. See 10.1.3 Variable Instantiation (ECMAScript Language
Specification).
I don't know exactly what "there's no chance to" means, but I believe
what scripts.contact meant was that in each iteration the variable
"name" is overwritten.

If that is so, it is incorrect - declaring a variable a second time
within the same scope has no effect on its value. Read the reference
cited.

>So unless name is used before the next iteration of the loop it is lost.

That makes no sense at all - declared variables are instantiated
before execution begins. Whether a particular iteration of a loop
assigns a value or not makes no difference to the variable's existence
(the case is quite different for undeclared variables though).
OK, I worded my response poorly. In this example:

(The original example.)
for(var i=0; i<list; i++)
{
var name = list[i];
}

When all iterations are complete, "name = list[i]" will surely not be
"name == list[0]", nor will you be able to retrieve it.

list = [0, 1, 2];
for (var i = 0; i < list.length; i++)
{
var name = list[i];
}
alert(name); // 2 and only 2, not 0, not 1

Unless I use name within that loop, before its next iteration, I cannot
access it. Unless of course I store it somehow or use a global variable.

Am I wrong?
>
> Hence it being "declared over and over again."

The declaration statement might be iterated over multiple times, but
the variable is effectively declared once and once only.
Right, I worded it wrong.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
May 1 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

24 posts views Thread by matty | last post: by
5 posts views Thread by altergothen | last post: by
6 posts views Thread by John Salerno | last post: by
2 posts views Thread by Rob Meade | last post: by
7 posts views Thread by Shapper | last post: by
30 posts views Thread by galiorenye | last post: by
21 posts views Thread by Markgoldin | last post: by
56 posts views Thread by mdh | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.