function functionScope(){ for(var index = 0; index < 10; index += 1){ //execute logic } //index is scoped to the function var newIndex = index - 10; }
variables are scoped to the nearest function
function blockScope(){ for(let index = 0; index < 10; index += 1){ //execute the same logic } //ERROR: index is scoped/contained to the for loop let newIndex = index - 10; }
variables are scoped to the nearest block
function usingConst(){ const message = "Success" const messages = { success: "Success", error: "Error" } //ERROR: message is constant message = "Error" //VALID: const is not recursive messages.success = "Error" }
not recursive