i is not incremented within JavaScript for loop [duplicate] - javascript

This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Javascript: Why do I need to declare var i = 0 in the for loop?
(4 answers)
Closed 4 years ago.
for (i = 1; i < this.people.length; i++) {
peoplePicks[i] = this.people[i].chooseAction(peopleChoices[i]);
}
I have this for loop within my JavaScript program. It runs for ever, even though the length of the array that I am passing to it is 2. when I print the value of i after the statement of the for loop, I get 0. So it seems like i is being decremented by executing the for loop's statement. How can I fix this?

Add var before your i variable in the initialising of your for loop.
Like this for (var i = 1; i < this.people.length; i++) {

Related

Why the output is different for both the loops? [duplicate]

This question already has answers here:
What is the difference between "let" and "var"?
(39 answers)
Closed 3 years ago.
I expected same output from the two loops
var arr = [5,6,7,8]
// first loop
for(var i=0;i<arr.length;i++) {
setTimeout(()=>{console.log(i,arr[i])},500)
}
// second loop
for(let i=0;i<arr.length;i++) {
setTimeout(()=>{console.log(i,arr[i])},500)
}
does let and var can change the closure property of any function especially in this case?
This is because of the lexical scope.
let will keep the variable value but var will update the value even before the first setTimeout callback will get executed.

Why does this program output “11” in a loop when use var and with let if works? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
let keyword in the for loop
(3 answers)
Closed 4 years ago.
Why output is 10 when use var and with let it works the loop.
Edit the output is 11 but why let and var are different
var funcions = [];
for (var x = 0; x <= 10; x++) {
funcions.push(function() {
console.log(x);
});
}
funcions.forEach(
function(func) {
func();
}
);
I know that let, is a signal that the variable may be reassigned, such as a counter in a loop, or a value swap in an algorithm. It also signals that the variable will be used only in the block it’s defined in, which is not always the entire containing function.

Why do var and let produce different outputs in closures? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
What is the difference between "let" and "var"?
(39 answers)
Closed 5 years ago.
Consider these 2 snippets:
With let:
for(let i = 0; i < 10; i++) {
setTimeout(function() {
console.log(i);
}, 100)
}
With var:
for(var i = 0; i < 10; i++) { //HERE
setTimeout(function() {
console.log(i);
}, 100)
}
From my understanding, let only affects the scoping of the variables, so why are the outputs of the 2 loops should be same, so why does let print 0-9 and var prints 10 10 times?
Because you have a timeout in your loop, none of your logs are being called until the entire loop is finished.
After the loop is finished, the value of i is 10 if you use var.
But let is block scoped, meaning it's the same value anywhere in the loop, even in an async function like setTimeout (unless you manually change it inside the loop) - because the loop creates a new scope on each iteration, it's almost like each iteration creates a new "instance" of the i variable when you use let, whereas using var each loop is using the same instance of the i variable.

Why does the first loop result to 0 instead of 1? [duplicate]

This question already has answers here:
trivial for-loop needs an explanation
(4 answers)
Closed 5 years ago.
I don't understand why the first loop prints out 0 instead of 1. Doesn't the i++ apply to the first loop?
for (i = 0; i < 3; i++) {
console.log('i');
}
No i++ increments only after executing logic in the loop

Why do functions defined in a loop all return the same value? [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 7 years ago.
I'm confused by the following JavaScript code. I wrote a loop and defined a function inside the loop, but when I call a function defined in the loop, I only get 10 rather than the index.
Obviously, in the following code I abstracted out stuff that isn't relevant:
objectArray = [];
for (i = 0; i< 10; i++){
objectArray[i] = {};
}
for (i = 0; i< 10; i++){
objectArray[i].get_number = function(){
return i;
}
}
console.log(objectArray[5].get_number()); // returns 10 rather than 5
I always get 10 rather than i as expected.
It's because of JavaScript closure. The method objectArray[i].get_number has direct access to i (not a copy of i). The value of i lives on in memory because each method objectArray[i].get_number still has access to it. There's only one i, and when the loop iterates it, it increases by one. When a method objectArray[i].get_number accesses it, the loop has already run to completion, so it accesses the final value of i.

Categories