The declaration of variables in JS

Source: Internet
Author: User

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

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


}
Alert (t), theanswer is undefined, why, because the variable declaration is in advance, so t is inside the window object, but did not go the following judgment, so there is no assignment, 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, the value of num + 1 is undefined, so the answer is Nan after the operation, remember when we look for the object is

Level to look up, can not 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 precede the same name parameter, so is Var, FN,FN, and then Renturn, did 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, we start with Var, then the function body, and finally the argument

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.