Clarifying the var declaration documentation

The present reference page for var declarations says, under ‘Description’:

Variable declarations, wherever they occur, are processed before any code is executed.

The problem is that for newer JavaScript programmers (e.g. me), it’s not clear if this statement includes let and const declarations. A “variable declaration” could specifically mean a var declaration, but at the same time, let makes variables too, so a “variable declaration” could include let declarations.

I’d like to clear this up on the var page as well as in any other place I can find where it might be ambiguous, but first I need to know which is true:

  • Are only var declarations, wherever they occur, processed before any code is executed, or
  • Are var, let, and const declarations, wherever they occur, processed before any code is executed.

Or any mixture thereof.

Thanks!

@fscholz can you look into this? Thanks!

Only var declarations are processed before any code is executed, see also var hoisting.
The let and const declarations are different, see what is called a Temporal Dead Zone.

The page you quoted is the reference page about the var declaration, so it doesn’t necessarily refer too much to let. Maybe some clarifications couldn’t hurt, though.
The JS Guide tries to compare the three a bit more: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types#Variables

Thanks for contributing to MDN!

1 Like