I feel that the true answer to this question is often abstracted by the term "lift". This is what REALLY happens.
Each time a function runs in JavaScript, a new execution context is created for that function, which is pushed onto the execution stack. So, in the second example, you have the following code:
var me = 1; function findme(){ if(me){ var me = 100; console.log(me); } console.log(me); } findme();
At the very beginning of the execution of this code (assuming that it is all JavaScript), the first thing that happens is that the JavaScript engine creates a global execution context. During its creation, it allocates memory for all functions and variables in this context. Functions are allocated and initialized as you can imagine, but variables are only allocated first (not correctly initialized).
So, after creating the global execution context in this example, the findme () function will be allocated in its entirety, but the "me" variable will be selected with the initial value undefined. After creating the global execution context, the code runs. He finds var me = 1; and updates the variable "me" from undefined to 1. It really does nothing with the definition of the function, but then finds the invocation function "findme ();".
At this point, we introduce the findme () function. And indeed, the same thing happens again and again. An execution context is created for this function, which is pushed onto the global execution stack, and this becomes executable code. If you understand the first part of my discussion, I think you will understand that "console.log (me)"; the call after the if statement is undefined. What for? Since when creating the execution context of the findme () function, he initially created the variable "me", because he saw in his context "var me = 100;" expression. However, if if statements do not receive allocated execution contexts in terms of the global execution stack. Thus, when "console.log (me)"; OUTSIDE of if runs the IS variable defined in the general context of the function. BUT you are trying to access a variable outside its scope.
If you followed all this, now you understand the main part of work of the JavaScript mechanism.