DEV Community


Posted on


What's the difference between using "let" and "var"?

Scoping rules

The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope).

function run() {
  var foo = "Foo";
  let bar = "Bar";

  console.log(foo, bar); // Foo Bar

    var moo = "Mooo"
    let baz = "Bazz";
    console.log(moo, baz); // Mooo Bazz

  console.log(moo); // Mooo
  console.log(baz); // ReferenceError

Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Visualizing Promises and Async/Await 🤓

async await

☝️ Check out this all-time classic DEV post on visualizing Promises and Async/Await 🤓