You can access them before they are declared. In such case, their value would be undefined though, as only declarations and not initializations are hoisted. This is generally considered a bad practice. … You can access it only after it was declared.
How do you prevent hoisting?
Some ways to avoid hoisting are:
- Use let or const — As explained above, using let or const instead of var would throw an exception and not let the program run, hence helping catch the issue earlier.
- Use function expressions instead of function declarations.
Can const be undefined?
One of the nice features of const is that you must assign an initial value to the variable const myVariable = ‘initial’ . The variable is not exposed to the uninitialized state and accessing undefined is impossible. length and half variables are assigned with a value once.