Difference in var and let

What is the difference between Var and let?

actually you should include const as well in this comparison.

let and const where introduced in es6, and they are block scoped:

if (true){
   let blockScope = 'hello world';
   var functionScope = 'hello world';
   console.log(blockScope, functionScope);


logging functionScope outside the if clause goes fine, attempting to log blockScope outside the if clause (go ahead, try it), will result in an error

so a block is anything between {}. So var and let have very different scope

as for constant, they also have block scope, but they constant, once a constant variable is declared, it can’t be re-assigned:

const example = 'this goes fine';
example = 'attempting to re-assign, will result in an error';

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.