"Syntax Basics" Javas declaration and use of Cript variables
A variable is a container used to hold information.
Declaration of a variable
Javas cript uses the keyword var to declare a variable, you can either declare it, assign it, or assign a value at the same time as the declaration, and multiple variables are declared separated by commas (,). For example:
1
2
3
4
5
6
7
8//First declaration, Post assignment
var sex;
sex= "Male";
Assignment at the same time as the declaration
var age=22;
var name= "Zhang San";
Declare multiple variables at the same time
var x=1,y=2,z=3; To declare multiple variables separated by commas (,)
Among them, sex, age, name ... Called the variable name, "male", 22, "Zhang three ... is called a variable value.
Javas Cript is a weakly typed language that declares a variable without declaring a data type, Javas Cript automatically determines the data type based on the contents of the variable.
Javas cript variable Naming conventions: variables must begin with letters, $, and _, and cannot begin with numbers and other characters.
Note: The Javas cript is case-sensitive, and the variable age is not equal to age.
Use of variables
After the variable declaration, you can use it.
For example, two variables x and Y are declared:
1
2var x=2;
var y=3;
To get the X+y value, you can:
1
2var z=x+y;
document.write (z);
Run the code and output 5.
Unassigned Variable
An unassigned variable is a variable that has been declared with the Var keyword, but has no assigned value.
In Javas cript, an unassigned variable has a default value, which is undefined, which is undefined. For example:
1
2var x; x = undefined
alert (x);
Run the code, pop up the warning box, and show undefined.
Note: Unassigned variables are not equal to undeclared variables. In Javas Cript, referencing an unassigned variable with a value of undefined, referencing an undeclared variable throws an error.
For example:
1alert (XYZ);
Running the code without popping the warning box and opening the Chrome debugging tool (F12), you can see that the following error has been raised:
Uncaught REFERENCEERROR:XYZ is not defined
That is, "uncaught reference error: XYZ is not defined."
Javas Cript Notes
Javas Cript supports single-line comments and multiline comments: single-line comments begin with//and multiline comments start with/* and end with */.
Code that is commented out is not executed by the browser.
Single-line Comment:
1
2document.write ("single-line comment"); Here is a single-line comment
Alert ("Popup Window");
Multi-line Comments:
1
2
3
4
5/*
document.write () example
The following code will output "multiline comment"
*/
document.write ("multiline comment");
In the actual project development, it is necessary to comment appropriately. Annotations can not only explain the Javas cript code, improve the readability of the code, convenient two development, you can also comment out some Javas cript statements, to debug the code.
Note: Javas Cript does not support multi-line nested annotations. For example, the following comment is incorrect:
1
2
3
4/*
Note 1
/* NOTE 2 */
*/
"Syntax Basics" Javas declaration and use of Cript variables