The difference between adding var in JS and not adding it

console.log(a); // undefined
console.log(b); // b --------->
b = 10;
var a = 20;

***************************************

console.log(a); // undefined
b = 10;
console.log(b); // 10, 
var a = 20;

declare that this is all done in non-strict mode.
in the global context, why does the output of b report an error? it is said on the Internet that the attribute of b is a global object and not a global variable. Why? The second question is that a global object is an object that is created before entering any execution context, then the context phase for variable declaration, and finally the code execution phase, but why does b hang under the window only during the code execution phase?

Feb.28,2021

does not quite understand what the subject wants to ask. It can only be said that if b has not been declared before, then this writing will cause jvm to declare b for you in non-strict mode, and then assign a value. In strict mode, an error will be reported directly, but there is no declaration after all. If b was previously declared in the outer or current layer scope, then only the original b value has been modified.

it is recommended to check the strict Mode.

in addition, I seem to have heard that jvm will declare the variables in a script before executing it. This passage is based on my impression and is for reference only.

Of course, the output of

b will report an error, because you haven't declared b at the time of output, unless you console.log (b = 1) , then you will declare b before output. Even if jvm will take a look at the code for you and find that you declare b below, it will help you declare in advance that b is not in line with the program specification, and no normal, production program will be used first and then declared.


give a straightforward example

 function Example(){
    var a = 1  //
 }
 
 
 (function(){
     b=1
 })()
 (window)  b   var b;

a can be printed because the variable is promoted, b is not declared, so it will look up for the value of the variable named b, because it has not been declared before, it can not be found in the stack, so it will report an error.
when the code executes to b, the unwritten var, will look up to see if there is a definition. If so, the expression will be assigned. If no corresponding variable is found at the top level, b will be used as the attribute of the windows object and assigned.

clipboard.png

:

clipboard.png

An obj, as a global variable is printed before it is declared, and when it is found, undefined,
is then declared and assigned, in the printout {}, the final output underlined is the return value of var obj = new Object ().
due to variable promotion, although the first line of code does not define obj, it is actually split into this at run time

var obj = undefined// global variable declare
console.log (obj) / / undefined
obj = new Object () / / variable assign
console.log (obj) / / {}

as for b, there is no declaration for him in the code, and of course it will not be promoted, but when it runs here, it will look up as if b had already lived in the previous part, and when you find the top layer, that is, the window object, um, hang it to XD

.
  1. look at it this way, an is declared, but not defined

    var a;
    console.log(a); // undefined
    console.log(b); // b 
    b = 10;
    a = 20;
  2. b is not defined and is directly mounted under window
  3. .

unwritten var,js looks for the variable in the current context, and mounts it into window if it is not defined, and if so, it is a global variable. That's it


first of all, the variable initialization phase, var a, currently has only a. Then we enter the code block execution phase, where console.log (a) is the a variable that already exists, with a value of undefined. B does not exist

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b30e2a-2bd5a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b30e2a-2bd5a.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?