JavaScript variables declare outside or inside loop?

davivid :

In AS3 I believe you should initialise all variables outside loops for increased performance. Is this the case with JavaScript as well? Which is better / faster / best-practice?

var value = 0;

for (var i = 0; i < 100; i++)
{
    value = somearray[i];
}

or

for (var i = 0 ; i < 100; i++)
{
    var value = somearray[i];
}
bobince :

There is absolutely no difference in meaning or performance, in JavaScript or ActionScript.

var is a directive for the parser, and not a command executed at run-time. If a particular identifier has been declared var once or more anywhere in a function body(*), then all use of that identifier in the block will be referring to the local variable. It makes no difference whether value is declared to be var inside the loop, outside the loop, or both.

Consequently you should write whichever you find most readable. I disagree with Crockford that putting all the vars at the top of a function is always the best thing. For the case where a variable is used temporarily in a section of code, it's better to declare var in that section, so the section stands alone and can be copy-pasted. Otherwise, copy-paste a few lines of code to a new function during refactoring, without separately picking out and moving the associated var, and you've got yourself an accidental global.

In particular:

for (var i; i<100; i++)
    do something;

for (var i; i<100; i++)
    do something else;

Crockford will recommend you remove the second var (or remove both vars and do var i; above), and jslint will whinge at you for this. But IMO it's more maintainable to keep both vars, keeping all the related code together, instead of having an extra, easily-forgotten bit of code at the top of the function.

Personally I tend to declare as var the first assignment of a variable in an independent section of code, whether or not there's another separate usage of the same variable name in some other part of the same function. For me, having to declare var at all is an undesirable JS wart (it would have been better to have variables default to local); I don't see it as my duty to duplicate the limitations of [an old revision of] ANSI C in JavaScript as well.

(*: other than in nested function bodies)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Declare an object inside or outside a loop?

Is it better to declare a local inside or outside a loop?

C++: Declare a vector inside or outside of a loop

Declaring variables inside or outside of a loop

Inside and outside of for-loop in JavaScript

Is it faster to declare variables for loop criteria outside of the loop condition?

Multiple declarations same of variables inside and outside for loop

Declaring variables inside or outside in a for-in loop

Scope of Variables: Instantiation Inside or Outside Loop

Declaring div variables inside or outside of a loop with appendChild

Should we declare methods inside or outside the constructor in a class definition in JavaScript?

Conditionally chaining promises inside and outside of a loop in javascript

Should variables be declared inside the loop or outside the loop in java

variables declared outside of while loop not accessible inside the while loop in scala

Access array variables outside for loop javascript

How to declare dynamic variables inside FOR loop in OR-TOOLS

Declare variables outside of Erlang functions

Javascript loop-index declaration outside or inside loop-body?

JavaScript: Can I declare variables inside switch cases?

Using variables inside javascript for loop few times

Dynamic created variables inside for loop, Javascript

Access variables declared inside JavaScript switch statement from outside

Variables outside of while loop

Javascript Scope: variable declared inside as opposed to outside of a forEach loop

ES6 JavaScript - const inside or let outside loop?

Should I declare a variable inside a function or outside?

Dynamically declare and assign variables in a loop

How to declare datarow outside foreach loop?

Pass variables inside render to outside?