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

S

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;
}


or outside a loop routine, like this:


var i;
var name;

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

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
 
S

scripts.contact

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;

}


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;

}


OR:
for(var i=0,name;i<list;i++){ name=list }
----------^ : This expression is executed only once, before the loop
is executed
 
A

aksus-69

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).
 
A

aksus-69

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.
 
M

Matt Kruse

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
 
L

-Lost

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."
 
R

RobG

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.
 
L

-Lost

RobG said:
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.



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;
}

When all iterations are complete, "name = list" 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;
}
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?
The declaration statement might be iterated over multiple times, but
the variable is effectively declared once and once only.

Right, I worded it wrong.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top