JavaScript variable declaration in advance

Source: Internet
Author: User

The topic is as follows: Please write out the following code 2 times alert output what results separately?

<script type= "Text/javascript" >var a = 1; var A;alert (typeof a); (function () {b = ' Hello world '; var b;}) (); Alert (typeof B); </script>

My answer is: 1.undefined 2.undefined. Then leader let's think about the answer to the question. My analysis of the topic: 1. Declare a and assign a value of 1, and then re-declare a, but there is no assignment at this time, the variable is declared, but not assigned, so it is undefined. 2. The b variable in the first row is a global variable inside the function, and then a local variable is declared, but no value is assigned. The alert output is a global variable B, so it is also undefiend.

I ran the code myself in Chrome, and the correct result was 1.number 2.undefined. This is a review of JavaScript's variable declaration advance concept.

Test (); function test () {     alert ("Hello world!");}

The result of the operation is: Hello world!. Principle: Before the computer starts executing the statement, it finds all the function definitions and then saves the associated function.

Analysis results:

Question 1th:var a = 1; var A;

The 2nd line declares the variable a, which is equivalent to declaring a at the top, and then the first sentence is to re-declare a and assign a value of 1. So typeof A is number

Question 2nd: b = ' Hello world '; var b;

Second question: b= ' Hello World ', the program will first look inside the function to see if there is a declaration of variable B, if any, directly assigned to ' Hello World '. But alert ( typeof b); is outside the function, the output of global variable B, all is undefined.

Then 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 the results.

The analysis can be written in the following code snippet:

myname = "AAA"; function test () {        alert (typeof myname);//Find out if there is a myname declaration within the function, there is a declaration, but no value assigned. So typeof myname for undefined  var myname=123;//assignment Action   alert (typeof myname);//number} 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 of the program operation is: String,string. What is the reason?

Note: The results have been obtained through a joint discussion with the Friends of the park. As for the result of the last string and string, it is true that the Chrome browser defaults to name Name= "", so the result of the output is string. Here we are reminded that in the front-end development process, we are often concerned about CSS and HTML in the various browser compatibility, but sometimes JavaScript in the browser also has compatibility issues.

Reprinted from: Liminjun88 (including link http://www.cnblogs.com/liminjun88/)

JavaScript variable declaration in advance

Related Article

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.