Home > Article > Web Front-end > Discussion on defining variables and variable priority issues in JavaScript_javascript skills
Look at the code below:
Answer the following questions:
Will an error be reported? How many times will it pop up?
Is the second alert true or false?
What pops up in the third alert?
Why?
Think about it and then test it. If you answer it correctly, you don’t need to read the following articles.
---------------------------
It is too simple to define variables in JS, just use var, or even without var:
Let’s do something truly undefined:
That’s right, just alert a variable that has never appeared at all. What will happen?
Firebug reported an error directly: a is not defined. It means that a is not defined. This is confusing when combined with the previous code. What is the difference between this undefined and the previous undefined?
In fact, the previous code is equivalent to this:
That is to say, when a variable is declared without assigning a value, JS will pass an undefined value to the variable. Note that this is a "value", indicating that a already has a value. This value is called "undefined".
In the subsequent direct alert, the variable never appears, which means that this is truly undefined.
To put it simply: there is no variable without a value in JS, the variable is assigned a value when it is declared.
Then let’s look at the following code:
Will this code report an error? Because variable a has not had time to appear during alert.
But this way, no error was reported, but an undefined value popped up. It shows that variable a already exists, but the value is not what we want, but undefined. What problem is this?
Because var variable declaration is the same as function declaration, it will be advanced. In fact, the above code is like this:
Now you will understand.
So, the key to this problem is: the var statement will advance to the top of the scope, but the attached value will not - a very confusing setting, I don’t know why it is done this way. Personally, I think this is a flaw of JS.
Nowadays, there is a coding habit that advocates placing variable declarations at the front of the scope. This is probably because of this - anyway, even if you don't write it at the front, JS will go to the front in advance.
Now release the answer to the question at the beginning of the article:
Only two alerts will pop up, and the alert inside the if will not be executed. Because of the advance nature of the var declaration, the real code looks like this:
Although aa is empty, it will be true when judged by 'aa' in window, because a does exist and the value is undefined. So the if code will not execute. I won’t talk about the last two alerts.
Personally, I think this is a very nonsensical question. We should understand his reasons, but despise his trap.
The above question is also the reason why I wrote this article. I saw this code from an online article, but there is no answer in it. I couldn’t stand it, so I went to stackoverflow to ask. Figure it out. The answer is this article.
But this is a very basic question actually! ! !
Haha, forgive me, there is another question later: