473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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 7487
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
5
by: altergothen | last post by:
I'm currently doing a course in C# programming fundamentals. Please will you check and comment on the following assignment: Assignment: Create a simple calculator prgram that illistrates good OOP...
6
by: John Salerno | last post by:
This is from Programming in the Key of C#: "You can initialize multiple variables in the for loop: for (int i = 0, j = 0; ... But it's an either/or situation. If you declare one or more...
7
by: billr | last post by:
but ireally does need clearing up ... Traditionally (i.e. in C++) I might do the following: SomeObj ptrSomeObj; for(int i = 0; i < iCount; ++i) { ptrSomeObj = new SomeObj();...
2
by: Rob Meade | last post by:
Hi all, New to .Net - still finding my feet...quick question... In one of my functions I have about a dozen variables being declared at the top - the first thing within the function, about 2...
7
by: Shapper | last post by:
Hello, I have a "Select Case MyVar" in which I define the values of an Array according to the value of MyVar. I need to use the Array Values in a Loop after End Select. It seems the Array is...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
21
by: Markgoldin | last post by:
I am working on a program that creates a few TCP listenrs to accept data from non .net processes. Each listener listens in its own thread. I am using Thread.Sleep(1000) to let other threads work....
56
by: mdh | last post by:
As I begin to write more little programs without the help of the exercises, little things pop up that I need to understand more fully. Thus, below, and although this is not the exact code, the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.