Describes javascript variables and identifiers, and javascript variables.

Source: Internet
Author: User

Describes javascript variables and identifiers, and javascript variables.

I. Variables

Literally, variables are variable quantities. From a programming perspective, variables are containers used to store data.

1.1 variable features
The variables in javascript are loose and can store any type of data. Because there is no rule to define the data type value that a variable must save, the value of the variable and its data type can be changed within the lifecycle of the script.

1.2 variable naming
Variables can be named at will, but they must follow the 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 example 6num // cannot start with a Number % sum // cannot start with a special symbol except (_ $), such as (% +) except (_ $) and other special characters, such as (% +/), cannot start with sum + num)

[2] letters can contain expanded ASCII or Unicode characters, or Chinese Characters

[3] keywords, reserved words, true, false, and null cannot be used.

[4] variables are case sensitive

[5] identifiers should adopt the small hump format, and the first should be the data type. Common identifiers are as follows:

Array a Array aItems
Boolean value B Boolean bIsComplete
Floating Point f Float fPrice
Function fn Function fnHandler
Integer I Integer iItemCount
Object o Object oDIv1
Regular Expression re RegExp reEmailCheck
String s String sUserName
Variable v Variant vAnything

1.3 variable Declaration
Declaration format: var variable name;

Var num; // declare one variable var num1, num2; // declare multiple variables

Variables defined using the var operator will become local variables in the scope of the variables defined. If the var operator is omitted, a global variable can be created, but a ReferenceError is thrown in strict mode.

Var num1 = 1; num2 = 2; // in strict mode, num3 is returned; // an error is returned.

If you declare a JavaScript variable again, the value of the variable will not be lost.

var carname="Volvo";console.log(carname);//Volvovar carname;console.log(carname);//Volvo

1.4 statement escalation

The variable declaration in javascript will be promoted to all functions and statements, but the promoted variable will return undefined, because the Declaration is upgraded, and the value assignment operation is not promoted.

console.log(myvar); // undefinedvar myvar = "local value";console.log(myvar); // "local value" 

1.5 variable assignment
Use "=" to assign values to variables, that is, to store content. Variables can be assigned values during declaration, but other operations, such as + = and-=, are not allowed.

Var num = 5; // the upper and lower values are equivalent var num; num = 5; var a = 2; // correct var a + = 2; // error var a = 2 ++; // error, ++ can only be used for variables, not Constants

Ii. identifier

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

2.1 identifier name
The naming rules are the same as the variable naming rules. For attributes that do not comply with the naming rules, such as border-color, they should be written in braces ([borderColor]).

2.2 identifier resolution
Identifier Parsing is the process of searching for identifiers at the level of the scope chain. The search process always starts from the front end of the scope chain, and then goes back step by step until the identifier is found.

[1] If an identifier with the same name exists in a local environment, the identifier in the parent environment is not used.

[2] If the identifier cannot be found, it indicates that the identifier has not been declared, which usually leads to an error.

[3] The JavaScript engine has done a good job in optimizing the identifier query. the time difference between the access to the parent environment and the local environment identifier is negligible.

Var num = 1; function test () {num = 2; console. log (num); // 2 console. log (number); // error} test ();

The above is about the javascript variables and identifiers, and I hope to help you learn it.

Articles you may be interested in:
  • Multiple variable definitions in js (direct object volume, direct array volume, and direct function volume)
  • Js check variable type code ()
  • Js variables and their scopes
  • Introduction to the use of this variable in JS
  • How to call variables in JSP in JS
  • Error in JS missing identifier in IE
  • JS determines whether the variable is null and whether it is null
  • Js delete usage (delete object attributes and variables)
  • How to access external variables using JavaScript closure Functions
  • Dynamically set js property names using variables

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.