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

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.

Related

empty an array passed to a function [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Is JavaScript a pass-by-reference or pass-by-value language?
(33 answers)
Closed 3 years ago.
I am trying to empty an array passed to a function within javascript:
var a = [1,2,3];
function emptyArrayNo(ar) {
// the following does not empty the global array
ar = [];
}
function emptyArrayYes(ar) {
// any of the following 3 methods empties the global array
ar.length = 0;
//ar.splice(0, ar.length)
//while(ar.length > 0) {ar.pop();}
}
emptyArrayNo(a);
console.log(a); // returns [1,2,3]
emptyArrayYes(a);
console.log(a); // returns []
All the three methods within emptyArrayYes() seem to work, which I think is due to the reference to the array passed not being modified within the function. However when we set the array to [] within the function, I believe the reference changes therefore causing issues i.e. not being seen at the global scope. Can someone please clarify what is going on here? Thanks.

i is not incremented within JavaScript for loop [duplicate]

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++) {

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.

How each call finds the variable outside a for loop which includes a inner function? [duplicate]

This question already has answers here:
Explanation of `let` and block scoping with for loops
(5 answers)
How do JavaScript closures work?
(86 answers)
Closed 5 years ago.
Regarding the let Declarations in loops in book Definitive JavaScript Developer guideline, the code:
var funcs = [];
for (let i = 0; i < 10; i++) {
funcs.push(function() {
console.log(i);
});
}
funcs.forEach(function(func) {
func(); // outputs 0, then 1, then 2, up to 9
})
It says:
The let declaration creates a new variable i each time through the loop, so each function created inside the loop gets its own copy of it. Each copy of i has the value it was assigned at the beginning of the loop iterations in which it was created..
I want to know, when invoke the function func(), how and where it finds the i value for each element in array of funcs. All element in funcs are same
ƒ () {
console.log(i);
}
Could someone illustrate how each step happens when call the function in the above code? (I sort of understand that, if use var instead of let, the global variable i is what each call finds.)

Categories