Detailed JavaScript variables and identifiers _javascript tips

Source: Internet
Author: User

One, variable

In a literal sense, a variable is a variable amount; programmatically, a variable is a container for storing data

1.1 Variable characteristics
Variables in JavaScript are loosely typed and can hold any type of data. Because there is no rule that defines what data type value A variable must hold, the value of a variable and its data type can be changed during the life of the script

1.2 Variable naming
Variables can be named arbitrarily, but must follow naming rules:

[1] The first character must be a letter, underscore, or dollar sign. Other characters can be letters, underscores, dollar signs, or numbers

Error demonstration 6num//Beginning can not be used with
 digital
 %sum//Beginning can not use except (_ $) outside special symbols, such as (% +/etc)
 Sum+num//The beginning of the middle can not be used except (_ $) outside special symbols, such as (% +/etc.)

The letters in [2] characters can include extended ASCII or Unicode alphabetic characters, or you can use Chinese

[3] cannot use keywords, reserved words, true, false, and NULL

[4] variable is case sensitive

[5] The identifier should be in small hump format, the first digit should be the type of data, the common identification is as follows:

Array A-array aitems
Boolean B boolean Biscomplete
Floating-point number F float fprice
FUNCTIONS fn function Fnhandler
Integer i-integer iitemcount
Objects O Object ODIv1
Regular expression re RegExp Reemailcheck
Strings S String susername
Variable V variant vanything

1.3 Variable Declaration
The declaration format is: VAR variable name;

var num;//declares a variable
var num1,num2;//declares multiple variables

A variable defined with the var operator becomes a local variable in the scope that defines the variable. If you omit the var operator, you can create a global variable, but in strict mode it throws a Referenceerror error

var num1=1;
num2=2;//in strict mode will be the error
num3;//error

If you re declare a JavaScript variable, the value of the variable is not lost

var carname= "Volvo";
Console.log (carname);//volvo
var carname;
Console.log (carname);//volvo

1.4 Statement Promotion

A variable declaration in JavaScript is promoted to all functions and statements, but the promoted variable returns to undefined because it simply declares the elevation, and the assignment operation does not elevate

Console.log (MyVar); Undefined
var myvar = "Local value";
Console.log (MyVar); "Local Value"
 

1.5 variable Assignment
Use "=" To assign a value to a variable, which is to store the content. Variables can be assigned when they are declared, but they cannot have other actions, such as + =, =, etc.

var num = 5;
Up and down is equivalent to
var num;
num = 5;
var a = 2;//correct var a = =
2;//error
var a = 2++;//error, + + only for variables, not for constants

Second, identifiers

An identifier is the name of a variable, function, property, or a parameter of a function.

2.1 identifier naming
Named rule with variable naming rules, for attributes that do not conform to named rules such as Border-color should be written as curly braces [bordercolor]

2.2 Identifier resolution
Identifier Resolution is the process of searching for identifiers at one level along the scope chain. The search process always starts at the front end of the scope chain, and then goes back backwards until the identifier is found.

[1] The identifier in the parent environment is not used if there is an identifier in the local environment with the same name

[2] If the identifier is not found, the identifier is not yet declared, which usually results in an error

[3] The JavaScript engine does a good job of optimizing identifier queries, and the time difference between the identifier that accesses the parent environment and the local environment can be negligible

var num = 1;
function test () {
 num = 2;
 Console.log (num);//2
 Console.log (number);//Error
}
test ();

The above is about JavaScript variables and identifiers related content, I hope to help you learn.

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.