1

The following code produces the following output. But is it possible to keep the value "8888" for "index"? In other words, how do I make the for-each variable a local one? CODE:

var index = 8888;
console.log(index);
for ( index in a) { // by the way a.length = 5 
    console.log(index);
}

console.log(index);

OUTPUT:

8888
0
1
2
3
4
4
1
  • 4
    Use a different variable! OR wrap it in a IIFE Commented Apr 16, 2015 at 23:17

5 Answers 5

8

Just change the name of the variable.

Instead of

var index = 8888;
console.log(index);
for (index in a) { // by the way a.length = 5 
    console.log(index);
}

console.log(index);

Use

var index = 8888;
console.log(index);
for (i in a) { // by the way a.length = 5 
    console.log(i);
}

console.log(index);

When you write for (index in a) you create a local variable index, that overides temporarily the other variable index. You can change the name of the local variable for anything different than index, then you won't have this problem!.

Sign up to request clarification or add additional context in comments.

1 Comment

SImplest solution is to add a let/const. It's probably worth mentioning that you shouldn't be using a for in loop to iterate over arrays. Consider editing your example where you are creating a global i
4

Scope it with const or let:

var index = 8888; // `index` is 8888 here.

for(const index in a){ // `index` is "0", then "1", "2", "3", and "4" here.
  a[index];
}

// `index` is 8888 here again.

const and let make variables scoped to the block they are declared in: in this case a new index variable is scoped to each iteration of the for loop. All of them overshadow the outside index. After the loop ends the index variable declared with var becomes accessible again.

3 Comments

let is specific to the ES6 standard.
You might want to explain how to use this in current browsers.
Oh, yeah, I forgot to mention that… I use too much ES6 stuff myself lately, although I’ve never really used let… @FelixKling I’d just suggest one of the other answers.
1

if is mandatory to use same name, you should use self-executing anonymous JavaScript functions

var index = 8888; 
var a =[1,2,3,4,5];
console.log(index);
(function(){
  for (var index in a) { // by the way a.length = 5
    console.log(index);
  }
}());
console.log(index);

1 Comment

although I think it is better to rename the variable ..... @"Rafael Cardoso" solution
1

See this documentation on foreach: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

You can have something like this and avoid using your own index variable (the arrayIndex variable below is local to the callback function in the forEach):

var array = ['a', 'b', 'c'];
array.forEach(function (current, arrayIndex) { 
  console.log(arrayIndex); 
});

Comments

1

Scoping in Javascript is somewhat strange in that variables declared in an new scope will not shadow variables previously declared in a previous scope. Your question demonstrates the textbook example of what that means.

The most compatible way to do this is to just rename your variable in your for loop:

var index = 8888;
console.log(index);
for (i in a) { // by the way a.length = 5 
    console.log(i);
}

The next most compatible way is to use the forEach function:

var array = ['a', 'b', 'c'];
array.forEach(function (current, arrayIndex) { 
  console.log(arrayIndex); 
});

The ECMA6 way that is officially approved but may still not be fully implemented by browsers is to use the let keyword.

var index = 8888;
console.log(index);
for ( let index in a) { // by the way a.length = 5 
    console.log(a[index]);
}

console.log(index); // still 8888

From the documentation:

let vs var

When used inside a block, let limits the variable's scope to that block. Note the difference between var whose scope is inside the function where it is declared

It continues:

You can use the let keyword to bind variables locally in the scope of loops instead of using a global variable (defined using var) for that.

let is pretty much Javascript trying to make there scoping less crazy in a backwards compatible way.

1 Comment

@shmuli correct, my link shows exactly what browsers support that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.