What the difference between var and let

I need your help. I have 2 small pieces of code :

for (let i = 0; i < 4; i++) {
  setTimeout(() => console.log(i), 1000)
}

The result will be : 1 2 3 4

for (var i = 0; i < 4; i++) {
  setTimeout(() => console.log(i), 1000)
}

The result will be : 4 4 4 4

Can you explain why the results will be different and what is the difference between let and var? Thank you very much

HI @Halland !

Welcome to the forum!

The top stackoverflow answer has a great explanation of why this is happening in setTimeout and provides code examples