After lunch last Thursday, the leader sent us a JavaScript question to do. There are people in our team who work on the front-end, some people who work on the back-end, and some who work on mobile web, so everyone has a different understanding of the question. , and then discuss it in the QQ discussion group. Although the findings are very basic, I gained a lot through discussions and shared them. Of course, from the perspective of developers with development experience, these are the most basic things for learning JavaScript. Because I usually use jQuery or third-party JS components, I don’t pay enough attention to basic JavaScript learning. The title is as follows. The question is: What are the output results of the two alerts?
Please write the results.
Analysis can be written as the following code segment:
Copy code
The code is as follows:
name= "aaa";
function test(){
alert(typeof name);//Look inside the function to see if there is a declaration of name in the context, if there is a declaration. However, the assignment operation cannot be advanced, so the type is undefined
var name="bbb";//Assignment operation
alert(typeof name);//string}test(); But what is the result of running the following code snippet?
Copy code
The code is as follows:
The result of running the program is :string, string. I'm confused here and don't know how to analyze and explain. I think I understand the variable declaration in advance, but if I analyze the above code snippet using the learned method, I will get wrong results. So what does the assignment of variables have to do with whether they are outside the function (global variables) or inside the function (local variables)?