1. Variables
Literally, a variable is a variable quantity; from a programming perspective, 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. Since there are no rules that define what data type a variable must hold, the value of the variable and its data type can change during the lifetime of the script
1.2 Variable naming
Variables can be named arbitrarily, 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
//错误示范 6num //开头不能用数字 %sum //开头不能用除(_ $)外特殊符号,如(% + /等) sum+num //开头中间不能使用除(_ $)外特殊符号,如(% + /等)
[2] The letters in the characters can include extended ASCII or Unicode alphabetic characters, or Chinese
[3] Keywords, reserved words, true, false and null cannot be used
[4] Variables are case sensitive
[5] The identifier should be in camel case format. The first digit should be the type of data. Common identifiers are as follows:
Array
Boolean value b Boolean bIsComplete
Float
Function fn Function fnHandler
Integer i Integer iItemCount
Object Object oDIv1
Regular Expression re RegExp reEmailCheck
String s String sUserName
Variant v Variant vAnything
The declaration format is: var variable name;
var num;//声明一个变量 var num1,num2;//声明多个变量
var num1=1; num2=2;//在严格模式下会报错 num3;//报错
var carname="Volvo"; console.log(carname);//Volvo var carname; console.log(carname);//Volvo
The variable declaration in JavaScript will be promoted before all functions and statements, but the promoted variable will return undefined, because only the declaration is promoted, the assignment operation is not promoted
console.log(myvar); // undefined var myvar = "local value"; console.log(myvar); // "local value"
Use "=" to assign a value to a variable, that is, to store the content. Variables can be assigned values when declared, but cannot have other operations, such as +=, -=, etc.
var num = 5; //上下是等价的 var num; num = 5; var a = 2;//正确 var a += 2;//错误 var a = 2++;//错误,++只能用于变量,不能用于常量
Identifiers refer to the names of variables, functions, attributes, or function parameters
2.1 Identifier Naming
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]
Identifier resolution is the process of searching for identifiers level by level along the scope chain. The search always starts at the front of the scope chain and works backwards until the identifier is found.
[1] If an identifier with the same name exists in the local environment, the identifier in the parent environment will not be used
[3] The JavaScript engine has done a good job in optimizing identifier queries, and the time difference in accessing the identifiers of the parent environment and the local environment is negligible
var num = 1; function test(){ num = 2; console.log(num);//2 console.log(number);//报错 } test();