A JavaScript variable declaration of Knowledge Points _ basics

Source: Internet
Author: User

Last Thursday after lunch, leader sent a JavaScript topic for us to do, our team has to do the front-end, have to do backstage, also have to sit mobile web, so everyone's understanding of the topic is different, and then in the QQ discussion group inside the discussion. Although the discovery is very basic, but through the discussion harvest a lot, share out. Of course, in developers who have developed experience, these are the basics of learning JavaScript. Because usually use jquery or the third JS component, so the basic learning of JavaScript is not enough attention. The question is: What results are 2 alert outputs separately?

Copy Code code as follows:

<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 leader let's think over the answer to the question. My analysis of the topic:
1. Declare a and assign a value of 1, and then use the declaration of a, but there is no assignment at this time, the variable default value should be undefined.
The 2.b variable is a local variable within the function, and alert outputs a global variable B, so it is undefiend.
I ran the code in Chrome myself, and the code turned out to be 1.number 2.undefined. This is a review of the JavaScript variable declaration advance concept.
We're looking at another example, like the following:
Copy Code code as follows:

Test ();

function Test () {
Alert ("Hello world!");
}


The program does not complain, only the results of the operation is: Hello world!. Principle: Before the computer starts executing the statement, it finds all the function definitions and then saves the related function.
Question 1th:
var a = 1;
var A;
Line 2nd declares variable A, which is equivalent to declaring a at the top, and then the first sentence is to re declare a and then assign a value of 1. So typeof a for number
Question 2nd:
b = '-----';
var b;
The second question: b= '-----', the program will first find out whether the context has a variable B declaration, if any, directly assigned to '-----'. But alert (typeof B); is outside the function, the output of the global variable B, all is undefined.
Note that the assignment operation on the variable is not advanced.
Then look at the following code snippet:
Copy Code code as follows:

<script type= "Text/javascript" >
Name= "AAA";
function Test () {
Alert (typeof name);

var name= "BBB";
Alert (typeof name);
}
Test ();
</script>

Please write the result.
The analysis can be written as the following code snippet:
Copy Code code as follows:

Name= "AAA";
function Test () {
Alert (typeof name);//find within the function whether the context has a declaration of name, with a declaration. But the assignment operation cannot advance, so the type is undefined
var name= "BBB";//Assignment operation
Alert (typeof name);//string
}
Test ();

But the following code snippet, what is the result of the operation?
Copy Code code as follows:

<script type= "Text/javascript" >
Alert (typeof name);
var name= "Hello World";
Alert (typeof name);
</script>

Program run result is: string,string. Here is dizzy, do not know how to analyze and explain. Indicates that I think I understand the variable declaration ahead of time, but with the learning method to analyze the above code snippet, I will come up with the wrong result. is the assignment of the variable related to the external (global variable) or function (local variable) of the function?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.