how to defferentiat asyn by given example? [duplicate]

**
Why same code but different output
**
**
In the below code i have given two exaple with litle changes but the output of code is very different.
Why javascrript give the differ output.
Please explain why so?
**

function asyncFun() {
    for (let counter=0; counter < 10; counter++) {
        setTimeout(() => {
            console.log(counter);
        }, 100);
    }
}
asyncFun();
//output
0
1
2
3
4
5
6
7
8
9

function asyncFun() {
   
    for (var counter=0; counter < 10; counter++) {
        setTimeout(() => {
            console.log(counter);
        }, 100);
    }
}
asyncFun();

//output
10
10
10
10
10
10
10
10
10
10