F
F
FaTaLcheG2019-05-11 16:02:11
JavaScript
FaTaLcheG, 2019-05-11 16:02:11

Correctly debugging in Chrome browser displays variables?

I am reading the Modern JavaScript Tutorial from the site https://learn.javascript.ru/
In one of the tasks "Which variable will be assigned a value?"

var value = 0;
      function f(){
        if(1){
          value = true;
        }else{
          var value=false;
        }
          alert(value);
      };

      f();

There is a comment on this task

Which variable will be assigned the value?
The result will be true, because var will be processed and the variable will be created before the
code is executed.
Accordingly, assigning value=true will work on a local variable, and alert
will display true.
The external variable will not change.
PS If there is no var, then the variable will not be found in the function. The interpreter will look for
it in window and change it there.
So without var the result will also be true, but the outer variable will change

But what is of interest is the debugger, which, at the moment the function ends, shows that the global variable value is set to true.
5cd6c7435c781156818511.jpeg
Or is it possible to find the value value = 0 in the global scope? in scope tab

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stalker_RED, 2019-05-11
@Stalker_RED

There is such a thing as hoisting ( 1 , 2 )

else{
          var value=false;
        }

The function f has its own value variable, and when you do value = true, it is that variable that changes.
If you remove "var", then the closest value from the parent objects will be found. window.value will change.

P
profesor08, 2019-05-11
@profesor08

It looks like a glitch, it looks like the wrong info that is needed gets into the tooltip. Try reporting a bug. But if you give the variables different names, you will see that after executing the code, when hovering over the first variable, it will show that it is not declared. Most likely this is somehow related to the scope, and there are no variables with this name in the local scope.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question