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



var vs let vs const
var
is function scoped and if you try to use a variable declared withvar
before the actual declaration, you'll just getundefined
.const
andlet
are blocked scoped and if you try to use variable declared withlet
orconst
before the declaration you'll get aReferenceError
.- Finally the difference between
let
andconst
is that once you've assigned a value toconst
, you can't reassign it, but withlet
, 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