Learning javascript--variables and identifiers

Source: Internet
Author: User
Tags naming convention

Table of Contents [1] variable variable attribute variable named variable declaration variable assignment [2] Identifier identifier named identifier resolution variable

The variable is literally a variable amount; from a programmatic standpoint, a variable is a container for storing data .

Variable Properties

The 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 the variable and its data type can change over the lifetime of the script

Variablenaming

Variables can be named arbitrarily, but they must follow the naming conventions:

[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  // can not start with the number  // start not with the exception of (_ $) special symbols, such as (%  +/etc)  // You cannot use a special symbol other than (_ $) in the middle of the opening, such as (%  +/etc)

[2] The letters in the character 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] identifiers should be in small hump format, the first bit should be the type of data, the common identification is as follows:

Array a Aitems Boolean    b boolean biscomplete floating-point    f float fprice functions fn function  Fnhand    Ler integer i integer iitemcount objects  o object oDIv1 Regular expression    re  RegExp Reemailcheck string   s String sUserName Variable v variant vanything    

Variable declaration

The declaration format is: var variable name;

var num; // declaring a variable var num1,num2; // declaring 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 you will throw a referenceerror error in strict mode

var num1=1; num2= 2; // in strict mode will 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

Assigning values to variables

Use "=" To assign a value to a variable, that is, to store the content. Variables can be assigned at the time of declaration, but cannot have other actions, such as + =,-=, etc.

var num = 5; // the upper and lower is equivalent var  = 5;
var a = 2; // correct var a + = 2; // Error var a = 2++; // error, + + can only be used for variables, not constants

Identifier

Identifiers are names of variables, functions, attributes, or parameters of a function

Identifier naming

Naming rules with variable naming rules, for attributes that do not conform to a naming convention, such as Border-color, should be written as curly braces [bordercolor]

Identifier parsing

identifier parsing is the process of searching identifiers one level at a scope chain. The search process always starts at the front end of the scope chain and then goes backwards backward until the identifier is found .

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

[2] If an identifier is not found, indicating that the identifier has not been declared, usually causes an error to occur

[The 3]javascript engine does a good job of optimizing identifier queries, and the time difference between accessing the parent and Local environment identifiers can be negligible

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

Learning javascript--variables and identifiers

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.