A JavaScript variable declaration

  • 2020-03-26 21:36:46
  • OfStack

After lunch last Thursday, the leader sent us a JavaScript topic to do. In our Team, there are those who do the front end, those who do the background, and those who sit on mobile web, so we have different understandings of the topic, and then we will discuss it in the QQ discussion group. Although the discovery is very basic, but through the discussion gains a lot, shares. Of course, for experienced developers, these are the most basic things to learn about JavaScript. Since we usually use jQuery or the third JS component, we don't pay enough attention to the basic learning of JavaScript. The title is as follows, and the question is: what is the output of two alerts?


<script
type="text/javascript">
    var a = 1;
    var a;
    alert(typeof a);
    (function () {
        b = '-----';
        var b;        
    })();
    alert( typeof b);
</script>

My answer is: 1. Undefined 2. Undefined. Then the leader asked us to think about the answer again. My analysis of the topic:
1. Declare a and assign it a value of 1, and then redeclare a with no assignment at this time. The default value of the variable should be undefined.
2. Variable b is a local variable in the function, and the global variable b is output in alert, so it is undefiend.
I ran the code myself in Chrome and the correct result was 1.number 2.undefined. This is a look at JavaScript's concept of variable declaration in advance.
We are looking at another example, such as the following:

test();
function test(){
    alert("Hello World!");
}

The program doesn't report an error, it just runs: Hello World! . How it works: the computer looks up all the function definitions before it starts executing the statement, and then saves the associated functions.
Question 1:
Var a = 1;
Var a;
The second line declares the variable a, which is equivalent to declaring a at the top, and then the first line declares a again, and then assigns it to 1. So typeof a is number
Question 2:
B = '-';
Var b;
B = '-- ', the program will first find whether the context has a variable b declaration, if so, directly assign the value of '-- '. But alert(typeof b); It's the global variable b that's outside the function, and all of that is undefined.
Note that the assignment to a variable is not advanced.
Next look at the following code snippet:

<script type="text/javascript">
name="aaa";
function test(){
    alert(typeof name);

    var name="bbb";
    alert(typeof name);
}
test();
</script>

Please write down the result.
The analysis can be written as the following code snippet:

name="aaa";
function test(){
    alert(typeof name);//Look inside the function to see if the context has a declaration of name, yes. But the assignment cannot be advanced, so the type is undefined
    var name="bbb";//The assignment operation
    alert(typeof name);//string
}
test();

But what is the result of the following code snippet?

<script type="text/javascript">
alert(typeof name);
var name="hello world";
alert(typeof name);
</script>

The result is :string,string. Here is confused, do not know how to analyze and explain. It shows that I think I understand the variable declaration in advance, but by analyzing the above code snippet using the learned method, I will get the wrong result. So does the assignment of a variable have anything to do with whether it's outside the function (the global variable) or inside the function (the local variable)?


Related articles: