3 ms·
The only reason I can think of is if you want hoisting for some reason. For example, if you want to call functions before they are declared. Personally, I don
by mikekchar 9y ago
The only reason I can think of is if you want hoisting for some reason. For example, if you want to call functions before they are declared. Personally, I don't like that style, but some people do.
- Cthulhu_ 9y agolet / const are both hoisted though, they're just hoisted to the top of the current block scope instead of function scope.
- joshuacc 9y agoSort of, but the effect is very different due to the "temporal dead zone." Attempting to access a variable declared with `let/const` or const before the declaration will throw an error, vs just getting `undefined` for a variable declared with `var`.
- mikekchar 9y agoI'm a bit confused. What does "hoisted" mean in this context if accessing the variable before declaration will throw an error?
- beeandapenguin 9y agoFrom the MDN docs: "In ECMAScript 2015, let bindings are not subject to Variable Hoisting, which means that let declarations do not move to the top of the current execution context." https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let#Temporal_Dead_Zone_and_errors_with_let https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- scottmf 9y agoFunction declarations work like this typeof myFn === 'function'; // true function myFn() { }