You can mention binding as well. let has lexical scope binding (e.g. it binded inside for ), where is var has scope of function body:
let
for
var
function countdown(upto) { for (let i = 0; i < upto; i++) { setTimeout(() => console.log(i), 1000); } }; countdown(5); // Output: 0 1 2 3 4
Or using bind:
bind
function arrayOfNums(num) { var output = []; for (var i = 0; i < num; i++) { output.push((function(x) { return x; }).bind(null, i)); } return output; } arr[0](); // Output: 0
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can mention binding as well.
let
has lexical scope binding (e.g. it binded insidefor
), where isvar
has scope of function body:Or using
bind
: