Javascript variable declaration instance analysis

Source: Internet
Author: User

Javascript variable declaration instance analysis

 Javascript variable declaration instance analysis

This article mainly introduces javascript variable declaration. Examples of how to use javascript variable Declaration are analyzed. For more information, see

This article describes how to declare javascript variables. Share it with you for your reference. The specific analysis is as follows:

Declare a variable before using it in js. Variables are declared using the keyword var.

If the initial value is not specified for the variable in the var declaration statement, the variable value is undefined.

You do not need to specify the variable type when declaring the variable. js variables can be any data type.

It is legal and harmless to repeatedly declare variables using var statements. If you repeatedly declare an object with an initiator, it is no different from a simple value assignment statement.

If you try to read a variable that is not declared, js will report an error. In the strict mode of ECMAScript5, assigning a value to an unspecified variable also reports an error. However, historically, in non-strict mode, if you assign a value to an unspecified variable, js will actually create an attribute with the same name for the global object, and it seems to work like a correctly declared global variable. This means you are lucky to not declare global variables, but this is a bad habit that will cause many bugs. It is best to always use var to declare variables.

In a function, local variables with the same name overwrite the global variables.

Although you can write code in the global scope without writing var statements, you must use the var statement when declaring local variables. See the following code:

?

1

2

3

4

5

Scope = "global ";

Function foo (){

Scope = "local"

// Fk! We just modified the global variable !!!

}

In a C-language-like programming language, each piece of code in curly brackets has its own scope, and variables are invisible outside the declaration of their code segments, we call it block scope. js does not have block scope, but instead uses function scope ): variables are defined in the body of the declared function bodies and any function nested in this function body (whether nested inside or outside ?)

The function scope of js indicates that all the variables declared in the function are always visible in the function body, which means that the variables can even be used before they are declared. This informal feature of js is called hoisting. That is, all the variables declared in js functions (but not assigned values) are "advanced" to the top of the function body.

?

1

2

3

4

5

6

7

8

Var scope = "global ";

Function f (){

Console. log (scope );

// Output "undefined" instead of "global"

Var scope = "local ";

// Assign an initial value to the variable here, but the variable is defined anywhere in the function body.

Console. log (scope );

// Output "local"

The above code is equivalent:

?

1

2

3

4

5

6

Function f (){

Var scope;

Console. log (scope );

Scope = "local ";

Console. log (scope );

}

When declaring a js global variable, an attribute of the global object is actually defined.

When a variable is declared with var, this attribute cannot be configured when it is created, that is, it cannot be deleted using the delete operator; however, if you do not use the strict mode and assign a value to an undeclared variable, js will automatically create a global variable. The variables created in this way are the normal configurable attributes of the global object, yes:

?

1

2

3

4

5

6

Var x = 1;

Y = 2;

This. z = 3; // same as above

Delete x; // return false. The variable cannot be deleted.

Delete y; // return true. The variable is deleted.

Delete this. z // same as above

I hope this article will help you design javascript programs.

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.