What are the key differences between var, let, and const in JavaScript? When would you use each one?

JavaScript ImportantGoogleTekion

var vs let vs const

  • var is function scoped and if you try to use a variable declared with var before the actual declaration, you'll just get undefined.
  • const and let are blocked scoped and if you try to use variable declared with let or const before the declaration you'll get a ReferenceError.
  • Finally the difference between let and const is that once you've assigned a value to const, you can't reassign it, but with let, you can.
var vs let vs const

var:
function scoped
undefined when accessing a variable before it's declared

let:
block scoped
ReferenceError when accessing a variable before it's declared

const:
block scoped
ReferenceError when accessing a variable before it's declared
can't be reassigned
00:00

Table of Contents