How to understand closures

1. Use var:

var a = [];
for (var i = 0; i < 10; iPP) {
  a[i] = function () {
    console.log(i);
  };
}
a[6](); // 10

2. Use let

var a = [];
for (let i = 0; i < 10; iPP) {
  a[i] = function () {
    console.log(i);
  };
}
a[6](); // 6

question: in 1, the I in the console.log (i), inside the function assigned to the array an in the loop points to the global I, doesn"t the I of a [I] also point to the global I, and only a [10] has value?

Mar.03,2021

this question should not be related to closures. You should understand the scope of var and let.
take a look at the following example, and you'll see.

// 

let cons = (car,cdr) => (k)=> k === 1 ? car : cdr
let car = (cons) => cons(1)
let cdr = (cons) => cons(2)

// 
var cons2 = cons('2', '')
var cons1 = cons('1', cons2)

console.log(cdr(cdr(cons1))) // 
Menu