Hend El-Sahli
Hend El-Sahli

Reputation: 6752

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

var i;
for (i = 0; i < length; i ++) {
    // do something
}

OR

for (var i = 0; i < length; i ++) {
    // do something
}

is there any difference?

cause I've seen some famous js-modules are prefering the first one

Upvotes: 1

Views: 1421

Answers (6)

Mehrdad Kiani Anbohi
Mehrdad Kiani Anbohi

Reputation: 189

if you consider about performance, the first one is better, for performance of array, you have to set before your loop

let i = 0;
let len = array.length;
for (; i < len ; i ++) {
    // do something
}  

Look at the code above: When you change the length of the array in which you store the local variable, it varies greatly in performance and creates a higher speed in the loop, because the data storage in JavaScript is different and the speed of reading and writing in them is different, local variables They are read much faster than arrays.

where data is stored can greatly affect how quickly it can be accessed later. There are four basic places from which data can be accessed in JavaScript:

Literal values Any value that represents just itself and isn’t stored in a particular location. JavaScript can represent strings, numbers, Booleans, objects, arrays, functions, regular expressions, and the special values null and undefined as literals.

Variables Any developer-defined location for storing data created by using the var keyword.

Array items A numerically indexed location within a JavaScript Array object.

Object members A string-indexed location within a JavaScript object.

after that, one of the item, you can care about that is plus plus or minimize minimize, The loops in below example are reversed and combine the control condition with the decrement operation. Each control condition is now simply a comparison against zero. Control conditions are compared against the value true, and any nonzero number is automatically coerced to true, making zero the equivalent of false

    let len = works.length - 1;
    let i = len;
    for (; i >= 0; i--) {
    // do something;
}   

As a result, developers who care about system performance write code this way

Upvotes: 0

Fady
Fady

Reputation: 23

There is no difference at all. the first example is an old school of C language, but you may use it if you want to use the variable outside the loop (ex: to check it's value when the loop breaks).

the second is more common and better in readability, and as @Abdi2fa says: "It is always best to declare a variable within the smallest scope that contains all references to that variable"

Upvotes: 0

Ravi Jaiswal
Ravi Jaiswal

Reputation: 26

There is no difference in this two in JavaScript. In javascript there is concept of function scope not block scope

Upvotes: 0

Rahul Bhobe
Rahul Bhobe

Reputation: 4451

For var it does not matter and it will behave the same way either ways. In Javascript the variable declared with var can be hoisted.

If you are not using the variable outside the scope defined by the for loop, the current preference is to use let instead of var.

for (let i = 0; i < length; i ++) {
    // do something
}

Upvotes: 2

Abdi T. Bedaso
Abdi T. Bedaso

Reputation: 104

It doesn't matter how, they both get the job done!

Most programmers prefer the second one it is easy to identify and understand the initial values, assume having a bunch line of codes.


It is always best to declare a variable within the smallest scope that contains all references to that variable. If you do not need the loop counter outside of the loop, declaring it outside of the loop scope is unnecessary and bad practice.

Syntax the syntax explains itself clearly.

for ([initialization]); [condition]; [final-expression]) {
 // statement
}

Upvotes: 0

whartonweb
whartonweb

Reputation: 1

Option 2 is preferred to avoid an unnecessary global declarations

Upvotes: 0

Related Questions