0

Hoisting

what is javascript hoisting ?

11th Sep 2017, 5:34 PM
Naazer Ahmed Khan
4 Respostas
+ 12
stopping to use var is not practical let is a different thing than var. both have their functions should be used when necessary. always declare your variables at the top of your function or global scope else you will get undefined error. yes even with 'let' keyword https://code.sololearn.com/WVMVs8KdxDd0/?ref=app
11th Sep 2017, 6:08 PM
Lord Krishna
Lord Krishna - avatar
+ 5
Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. Inevitably, this means that no matter where functions and variables are declared, they are moved to the top of their scope regardless of whether their scope is global or local. Of note however, is the fact that the hoisting mechanism only moves the declaration. The assignments are left in place. If you've ever wondered why you were able to call functions before you wrote them in your code, then read on!
11th Sep 2017, 5:41 PM
MsJ
MsJ - avatar
+ 3
Hoisting is the terrible thing that haunts every javascript programmer in their sleep. In addition to what the others are saying: Stop using 'var'. Just never use it. In the new versions of Javascript (ES6 and newer) we have got 'let' that's the same as 'var', just without hoisting (well, almost). Use let instead of var and you'll be good!
11th Sep 2017, 5:52 PM
Schindlabua
Schindlabua - avatar
+ 3
Well I'm arguing 'var' is never necessary. Yes you should declare all variables on top of the scope, and actually let helps with that because you get an error when you try to use a variable that you declare later. If you use var? It's just undefined, the program continues, and you'll never know what hit you. No other language does what javascript's var does, it's just one of those warts that the language carried along for decades. The javascript committee realized that and gave us let :) var at this point is just there so old code doesn't break.
11th Sep 2017, 6:13 PM
Schindlabua
Schindlabua - avatar