1. Basic data type
There are four basic data types in java-script: numbers (integers and real numbers), strings (characters or numbers enclosed by ""or ""), Boolean values (true or false) and null. The data in the basic type of java-script can be constants or variables. Because java-script is a weakly typed form, it is not necessary to declare variables or constants of data first, but to determine the type of its data when using or assigning values. Of course, you can also declare the data type first, which will automatically explain its data type when assigning values.
2. Constant
(1) integer constant
Constants of java-script are often called literal constants, and they are immutable data. Its integer constants can be expressed in hexadecimal, octal and decimal.
(2) Real constant
Real constants are expressed by integer part plus decimal part, such as 12.32, 193.98. It can be expressed by scientific or standard methods: 5E7, 4e5, etc.
(3) Boolean value
Boolean constants have only two states: true or false. Mainly used to explain or indicate a state or flag to explain the operation process. Not the same as C++. C++ can use 1 or 0 to indicate its state, while java-script can only use True or False to indicate its state.
(4) Character constant
One or more characters enclosed in single quotation marks (') or double quotation marks ("). For example, "This is a java-script book", "3245", "ewrt234234" and so on.
(5) null value
There is a null value null in java-script, which has no meaning. If you try to refer to an undefined variable, a null value will be returned.
(6) Special characters
Like C language, java-script also contains some special characters that can't be displayed. These characters start with a backslash (/). Often called control characters.
3. Variable
The main function of variables is to access data and provide a container for storing information. For variables, the naming, type, declaration and scope of variables must be clear.
Naming of (1) variables
The naming of variables in java-script is very similar to its computer language. Here, we should pay attention to the following two points:
A. It must be a valid variable, that is, the variable starts with a letter, and numbers such as test 1 and text2 can appear in the middle. Variable names cannot have spaces, (+), (-), (,) or other symbols except underscore (-) as hyphens.
B keywords in B.java-script cannot be used as variables.
There are more than 40 class keywords defined in java-script, which are used internally by java-script and cannot be used as the names of variables. For example, Var, int, double and true cannot be used as the names of variables.
When naming a variable, it is best to correspond the meaning of the variable with the meaning it represents to avoid mistakes.
(2) Types of variables
In java-script, variables can be used with the command var:
var mytest
This example defines a mytest variable. But it is not assigned a value.
Var mytest= "This is a book"
This example defines a mytest variable and gives it a value.
In java-script, variables can be used without declaration, and the type of variables can be determined according to the type of data. For example:
x= 100
y=" 125 "
Xy= true
Cost = 19.5, etc.
Where x is an integer, y is a string, xy is a boolean type and cost is a real number type.
(3) Declaration of variables and their scopes
JavaScript variables can be declared and assigned before use. Use the var keyword to declare variables. The biggest advantage of declaring variables is that you can find errors in the code in time; Because java-script is compiled dynamically, and it is not easy to find errors in the code, especially in variable naming.
There is another important place for variables-the scope of variables. There are also global variables and local variables in java-script. Global variables are defined outside all function bodies, and their scope of action is the whole function; However, local variables are defined in the function body, and only the function is visible to them, while other functions are invisible.
Three. Expressions and operators
1.
After a variable is defined, it can be assigned, changed and calculated. This process is usually called an expression, which can be said to be a collection of variables, constants, booleans and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, assignment expressions and Boolean expressions.
2. Operators
A series of symbols by which operators perform operations. In java-script, there are arithmetic operators, such as+,-,*,/and so on. There are comparison operators such as! = = = and so on. ; There are logical Boolean operators such as! (Inverted), |, | |; There are string operations such as+and+=.
There are mainly binocular operators and monocular operators in java-script. Its binocular manipulator consists of the following parts:
Operand 1 operator operand 2
In other words, it consists of two operands and an operator. For example, 50+40, "this"+"that" and so on. A monocular operator needs only one operand, and its operator can be in front or behind.
(1) arithmetic operator
Arithmetic operators in java-script include monocular operators and binocular operators.
Binocular operators:+(addition),-(subtraction), * (multiplication),/(division),% (modulus), | (bitwise OR),&; (bitwise AND), << (move left), > > (move right), >>& gt (move right, zero padding).
Monocular operators:-(negative), ~ (complement),++(increase 1),-(decrease 1).
(2) comparison operators
The basic operation process of comparison operator is to compare its operands first, and then return a true or false value. There are eight comparison operators:
(3) Boolean logic operators
Added several Boolean logic operators in java-script:! (negative),&; Assignment after and),&; (Logical AND), | = (or post-assignment), | (logical OR), = (post-XOR assignment), (logical XOR),? : (ternary operator), || (or), = = (equal to), | = (unequal to).
Among them, the main formats of ternary operators are as follows:
Operand? Result 1: Result 2
If the result of the operand is true, the result of the expression is the result 1, otherwise it is the result 2.
Fourth, examples.
The following is a JavaScript document with subtitle effect.
Test2_ 1.html
& lthtml & gt
& lthead & gt
& lt scripting language = "Java-script">;;
Var msg= "Welcome to the website of Shanxi Window (www.shanxiwindow.net)";
Var interval =100;
var space len = 120;
var space 10 = " ";
var seq = 0;
Function scrolling () {
Len = msg. Length;
window.status = msg.substring(0,seq+ 1);
seq++;
If (sequence & gt= len) {
seq = spacelen
window . settimeout(" scroll 2();" , interval);
}
other
window . settimeout(" Scroll();" , interval);
}
Function Scroll2() {
var out =
for(I = 1; I< = spacelen/space10.length; i++) out +=
space 10;
out = out+msg;
Len=out. Length;
window . status = out . substring(seq,len);
seq++;
If (sequence > = len) {seq = 0; };
window . settimeout(" scroll 2();" , interval);
}
Scroll ();
& lt/script & gt;
& ltbody & gt
& lt/body & gt;
& lt/html & gt;