JavaScript learning (variables)

The concept of variables

Variables: Variables can be used to define independent variables, and the value of the variable can be changed arbitrarily

Definition and assignment of variables

Use var to declare a variable in js.
Examples in variables are as follows:

var a = 100;//赋值式声明
var b;//单纯的声明
var $abc;
var _abc;
var 1vg;//不允许数字开头
var .abc;//不允许$ 或 _之外的符号开头

as the picture shows

Insert picture description here
In the above figure, var is the keyword and
a is the variable name.
Note: A space must be used between the keyword and the variable name

Var is the abbreviation of "vraiant variable" in English. A space needs to be added after the space. The thing after the space is the "variable name":

  • Define variables: var is a keyword used to define variables. The so-called keywords are small words with special functions. The keywords must be separated by spaces;
  • Variable assignment: the equal sign means assignment, the value on the right side of the equal sign is assigned to the value on the left;
  • Variable name: It must be $ or _ or a letter as the beginning, and the variable name can also include it.
    PS: In JavaScript, var is always used to define variables (before ES6), which is different from languages ​​such as C and Java.
    Variables must be defined before they can be used. For example, we do not set variables and directly output:
<script type="text/javascript">
   		console.log(a);
   	</script>

The console will report an error directly:
Insert picture description here
correct writing:

var a;//定义
a = 100;//赋值
console.log(a);//输出100

Experienced programmers will unload definitions and assignments together:

var a = 100; //定义,并且赋值100
console.log(a); //输出100

Variable naming convention

Variable names have naming conventions: they can only be composed of letters, numbers, underscores, and dollar signs $, and cannot start with a number, and cannot be a reserved word in JavaScript.
The following words are called reserved words, which means they are not allowed to be used as variable names:

break
case
catch
continue
default
delete
do
else
finally
for
function
if
in
instanceof
new
return
switch
this
throw
try
typeof
var
void
while
with

Uppercase letters can be used and are case-sensitive, that is, A and a are two variables inside.

var A = 100;//变量1
var a =250;//变量2

ps: Chinese can be used as variable names, but it is not recommended because low

Identifier

Identifier: In js, everything that can be named independently by us can be called an identifier.
For example: variable names, function names, and attribute names are all identifiers.
The variable naming rules for identifiers are the same as the naming rules for variables.
Important: Identifiers cannot use keywords and reserved words reserved in the script language , as follows:
keywords and reserved words:
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46384159/article/details/115292693