Block scope vs function scope
Në shqip: Fusha e bllokut dhe e funksionit
ShpjegimiSQ
let dhe const jetojnë vetëm brenda bllokut {} ku krijohen; var jeton në gjithë funksionin. Prandaj var brenda një for-i „rrjedh“ jashtë tij — një burim i vjetër gabimesh.
EnglishEN
let and const live only inside the {} block where they're created; var lives across the whole function. That's why a var inside a for “leaks” out of it — an old source of bugs.
Si ta mendosh
Si një bisedë brenda dhomës (let) kundrejt një njoftimi në gjithë shtëpinë (var).
Lexoje në anglisht
Like a chat inside one room (let) versus an announcement to the whole house (var).
Shembull kodijavascript
if (true) { var a = 1; let b = 2; }
console.log(a); // 1
console.log(b); // ReferenceError