This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 1 year ago.
var a = [];
var b = ['abc','def'];
for(var c of b) {
var c_ = c.slice();
a.push(function(){alert(c_)});
}
a[0]();
a[1]();
Result is seing the message "def" twice. This is entirely unexpected. How do I do this in JavaScript? If it were alert(c) I could understand but c_ is clearly a copy of c and is properly scoped by other language's standards.
var is always function scoped. let is block scoped!.
Related
This question already has answers here:
Javascript - Precedence in hoisting
(2 answers)
Closed 7 months ago.
var a=2;
function a(){};
console.log(typeof(a));
I am wondering why the result of the above code is number. I know the var is hoisted but isn't it already at the first line of the code? so why the type of a is still a number instead of a function?
In this case function a(){} is actually hoisted, which you can see if you log a at various points:
console.log(a);
var a = 2;
console.log(a);
function a() {
console.log('hi');
}
console.log(a);
This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
What is lexical scope?
(21 answers)
Closed 10 months ago.
I'm trying to access the variable c that is initialized inside myFunc when I call sum.
let sum = (a,b) => {
let result = a + b + c;
return result;
}
function myFunc(){
let c = 5;
return sum(1,2);
}
console.log(myFunc());
But I'm getting an error because of the c that I use inside the arrow function. Don't know if I'm explaining right but I think you get the point by seeing the code above.
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.
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.
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.