const. At some point you may need to touch ‘var’ code, could be via a library which you have to fix or long living code. That’s when it can become dangerous if you don’t know the behaviour of hoisting. It may also help you in interviews. It is kind of similar to the C89 ‘declare variables at the beginning of a scope block’ thing, but not really.
var and use it before that line. Initializations are not hoisted, so you could run into
undefined is not... issues if you are not careful.
undefined. Compared to Java and other strongly and statically typed languages (where an
int may be set to 0 initially), this is different. The point about this example is, that the declaration is moved to the top of the block. For the call in line 7 this can become really dangerous, because you could think that the value is still 42.
Compared to the usual function declaration, the named function expression in this example looks comparably strange. It is a special hoisting case, where the function’s name doesn’t get hoisted. In my opinion you should rather use named function expressions with arrow functions.
Hoisting is a not-so-well known concept anymore, since the
const keywords were introduced in the ES6 standard. Especially for redeclaring variables, the
let instead of
var for example.