Variables in javascript are declared through the var keyword (variable).
The code copy is as follows:
var school = "beijingyizhong"
You can also use the var keyword to give multiple values to the variable.
The code copy is as follows:
var school = "beijingyizhong" , diqu = "beijing" , age = 100;
In addition, unlike Java, JavaScript can store different data types in the same variable. For example
The code copy is as follows:
var school = "beijing";
document.write(school);
school = 132134;
document.write(school);
In addition, JavaScript can be used without declaring variables, for example:
The code copy is as follows:
var test1 = "i am teacher" ;
test2 = test1 + "PK yuan" ;
document.write(test2);
When JavaScript encounters undeclared variables, it will automatically create a global variable for them. For good use habits, variables should be declared, which is beneficial for work.
In addition, variable declarations should follow the following rules:
The first letter of a variable can be in upper and lower case English letters, underscores or US dollar "$" symbols
The remaining letters can be underscores, upper and lowercase letters, any number or US dollar "$" symbol
The name of a variable cannot be a reserved word or a keyword
Below are some legal variable names
The code copy is as follows:
var test;
var _beijing;
var $djas;
Below are some illegal variable names
The code copy is as follows:
var 4abcd ; //The beginning of the number
var asdf"dad ; //Single quotes are illegal characters
var false; // false is the keyword
The above is all the content of this article, which are all very basic knowledge. I hope you like it.