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))) // 
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1bf0431-31cee.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1bf0431-31cee.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?