Home > Web Front-end > JS Tutorial > body text

Knowledge points of a JavaScript variable declaration_Basic knowledge

WBOY
Release: 2016-05-16 17:18:44
Original
1048 people have browsed it

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?

Copy code The code is as follows:

type="text/javascript ">
var a = 1;
var a;
alert(typeof a);

(function () {
b = '-----';
var b;
})();
alert( typeof b);


My answer is: 1. undefined 2. undefined. Then the leader asked us to carefully consider the answer to the question. My analysis of the topic:
1. Declare a and assign the value to 1, and then redeclare a, but there is no value assigned at this time, then the default value of the variable should be undefined.
2. The b variable 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 of the code is 1.number 2.undefined. What is examined here is the concept of JavaScript variable declaration in advance.
We are looking at another example, such as the following:
Copy the code The code is as follows:

test();

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


The program will not report an error, but the running result is: Hello World!. Principle: Before the computer starts executing the statement, it will first search for all function definitions and then save the relevant functions.
Question 1:
var a = 1;
var a;
Declaring variable a on line 2 is equivalent to declaring a at the top, and then the first sentence is redeclaring a, and then Assign a value of 1. So typeof a is number
Question 2:
b = '-----';
var b;
Analysis of the second question: b='-----', program First, it will check whether there is a declaration of variable b in the context. If so, it will directly assign the value to '-----'. But alert(typeof b); is outside the function and outputs the global variable b, all of which are undefined.
Please note: assignment to variables is not done in advance.

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)?
Related labels:
source:php.cn
Previous article:Sharing several codes for JS closing window or JS closing page_javascript skills Next article:jquery and native js get the value selected in the select drop-down box example_javascript skills
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template