The declaration of variables in JS

Source: Internet
Author: User

We all know that the declaration of variables in JS is to advance, there are 4 examples:

1.if (! " T "in window) {
var t = 1;


}
Alert (t); The answer is undefined, why, because the variable declaration is ahead of time. So t is inside the window object, but does not follow the following inference. So there's no assignment, and the answer is undefine.

2.var num = 100;
function fn () {
var num = num + 1;
return num;
}
Falert (n ()); The answer is still nan, because in the function body first put the Var num ahead, so at the time of assignment, first go to the scope chain to find num This parameter is found in this function. But found that there is no assignment, the value in num + 1 is undefined, so the answer is Nan after the operation, remember when we look for the object is

Level to find above. I can't find the outside.

3.var B = (function () {
   function fn () {
return 1;
 }
 return fn ();
 FUNCTION fn () {
 return 2;
 }
 var fn;
 FN = 3
}) ();
Alert (b). The answer is 2, because the VAR fn is first advanced. Then the function body is defined as a function declaration. and the function declaration will be after the same name, so it is Var, fn,fn, and then Renturn, not go after the fn=3, so the answer is 2

4.function AA (A,b,c) {
function A () {}

Console.log (a);
Console.log (AA);
Console.log (arguments);
var a = "ee";
var AA = "444";
arguments = 6;
Console.log (a);
Console.log (AA);
Console.log (arguments);
}

AA (n/a); The answer is function A () {}

Undefined

[function,2,3]

Ee

444

6 when we understand the variable declaration. First it's Var, then the function body, and finally the number of references.

The declaration of variables in JS

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.