var
keyword. These variables get hoisted. Hoisting is JavaScript's default behaviour of moving all declarations to the top of the current scope (to the top of the current script or the current function). This means that the following code will worklet
keyword will work almost the same as var
but they will not be hoisted, so the following will NOT work.Uncaught ReferenceError: Cannot access 'y' before initialization
const
are called "constants" and just like those declared with let
, they are NOT hoisted. They also have another behaviour, which is that they cannot be reassigned. Once the variable has been declared, the value stored inside it cannot be replaced. Again, this might seem like odd restrictive behaviour, but it is also often desired - more often than not.let
.