Javascript Upgrade Questions

While I was reading about the rise of Javascript, I tried the following. I am not sure why the first and second are displayed differently. Thank you in advance. (I'm not even sure if this is due to the rise.)

var me = 1; function findme(){ if(me){ console.log( me );//output 1 } console.log( me ); //output 1 } findme(); 

However, the following output is undefined.

 var me = 1; function findme(){ if(me){ var me = 100; console.log(me); } console.log(me); } findme(); // undefined 
+6
source share
4 answers

Declaration variables go up at the top of each function, but assignments remain where they are. So, the second example is executed as follows:

 var me = 1; function findme () { var me; // (typeof me === 'undefined') evaluates to true if (me) { // evaluates to false, doesn't get executed me = 100; console.log(me); } console.log(me); } findme(); 
+6
source

Declaring your local variable in an if block in the second example will raise the whole function.

Thus, me in a function always refers to a local variable, not a global one.

However, the value of the variable does not rise, therefore it is always undefined and if never entered.

+3
source

Rising from var DOES NOT LIFE an assignment, just an announcement. Therefore, it is analyzed as follows:

 function findme(){ var me; if(me){ me = 100; console.log(me); } console.log(me); } 

When the if is executed, me decounted locally for the function, but it has the value undefined (does not matter). undefined is false, so your condition will never be true.

This is why it is usually always to declare local variables at the top of functions, because they go anyway if you like it or not.

+2
source

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(); // undefined 

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.

0
source

All Articles