At its core, hoisting is an "order of operations" issue. JavaScript code goes through two phases: compilation and execution.
Declarations (var, ...
For further actions, you may consider blocking this person and/or reporting abuse
I like to use this in try/catch statements as well as some if cases
some times you just want to go
constall the way but when you meet these cases using let doesn't feel attractive and in the end, you still need to declare the let variable.Example:
As with anything in code, as long as you and your team agree to do certain things it's okay. If you don't feel is right or your team doesn't want to go that road you can keep it for yourself
I am curious, why is your variable being mutated in the catch block? that block is for errors
A good example of when you might want to do this is if it’s a known exception and you are going to hand wave it away.
the times I need to do this is to show a default for something
or just ensure there is a valid value returned from the function, most of the time this is used on places where failure is not critical or it's a "top level" UI element function call
I'd say it's kind of fail behind scenes keep the good face up
While it is important to understand hoisting in JavaScript, it is also a bad practice to use it in general. I suggest that you treat JavaScript just like any other programming language and don't use hoisting.
I have a template that I follow for any script that write:
// Declare variables:
// Declare functions:
// Start of coding:
Having those three sections makes it much easier for me to keep track of what is in my script.
That is excellent advice and very true!
so many people are confused about what hoisting is. Let me start off by bursting the bubble with one statement
All declarations are hoisted (var, let, const, function, class)
You're absolutely right and that's a point I didn't make clearly enough in the article. Might merit an edit.
Your questions are addressing too many things. You are talking about scope, execution context, thread, compilation. These are things that you must understand fully. I think that you would benefit from watching this video. please pay for the full course, it will greatly improve your JavaScript skills. Tony teaches about all these things.
youtube.com/watch?v=Bv_5Zv5c-Ts
Super helpful! No more vars for me :P
Wow! Great summary and explanation of a complex and esoteric topic!