2. Basic JavaScript Data Structure
Programming in the scripting language is very similar to that in C ++. It only removes pointer-related errors in the C language and provides a powerful class library. For people who already have C ++ or C, it is very easy to learn JavaScript scripting language.
I. Adding JavaScript code
JavaScript scripts are included in HTML and become part of HTML documents. Combined with HTML tags, it forms a powerful Internet programming language. You can directly add JavaScript scripts to the document:
<Script language = "JavaScript">
JavaScript code;
JavaScript code;
....
</SCRIPT>
Note:
(1) Identify <SCRIPT>... </SCRIPT> to indicate that the Javascript script source code will be placed in it.
(2) The attribute Language = "JavaScript" is used to indicate the language used in the logo. Here it is the Javascript language, indicating the language used in JavaScript.
Ii. Basic Data Types
Like other languages, the Javascript scripting language has its own basic data types, expressions, arithmetic operators, and the basic framework structure of the program. Javascript provides four basic data types for processing numbers and text. While variables provide places where information is stored, expressions can complete complicated information processing.
1. Basic data type
In JavaScript, there are four basic data types: Numeric (integer and real number), string (character or value enclosed by a "" sign or), Boolean (expressed by true or false), and null. Data in the basic JavaScript type can be constants or variables. Javascript uses a weak type, so a data variable or constant does not need to be declared first. Instead, it determines the data type when using or assigning values. Of course, you can also declare the Data Type first. It automatically describes the data type when assigning values.
2. Constants
(1) Integer constants
A JavaScript constant is also called a literal constant, which cannot be changed. Its Integer constants can be expressed in hexadecimal, octal, or decimal notation.
(2) real constants
A real constant is represented by an integer plus a decimal part, for example, 12.32 or 193.98. It can be represented in scientific or standard methods: 5e7, 4e5, etc.
(3) Boolean Value
A boolean constant has only two States: true or false. It is mainly used to describe or represent a state or sign to describe the operation process. It is different from C ++. c ++ can use 1 or 0 to indicate its state, while javascript can only use True or false to indicate its State.
(4) scalar Constants
One or several characters enclosed by single or double quotation marks. Such as "this is a book of JavaScript", "3245", and "ewrt234234.
(5) Null Value
Javascript has a null value, indicating nothing. If you try to reference a variable without definition, a null value is returned.
(6) special characters
Similar to the C language, JavaScript also contains some special characters that cannot be displayed starting with a backslash. It is usually called a control character.
3. Variables
Variables are mainly used to access data and provide containers for storing information. For a variable, you must specify the name, type, declaration, and scope of the variable.
(1) variable naming
The naming of variables in Javascript is very similar to that in computer languages. Note the following:
A. It must be a valid variable, that is, the variable starts with a letter, and numbers such as test1 and text2 can appear in the middle. Except for the underscore (-), the variable name cannot contain spaces, (+), (-), (,), or other symbols.
B. You cannot use keywords in Javascript as variables.
More than 40 class key words are defined in Javascript. These keys are used internally in JavaScript and cannot be used as variable names. For example, VAR, Int, double, and true cannot be the names of variables.
When naming a variable, it is best to match the meaning of the variable with its meaning to avoid errors.
(2) variable type
In JavaScript, variables can be declared using 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 assigns it a value.
In JavaScript, variables can be unspecified, and the type of variables is determined based on the data type during use. For example:
X = 100
Y = "125"
XY = true
Cost = 19.5.
X integer, Y is the string, XY is the boolean type, and cost is the real type.
(3) variable declaration and scope
Javascript variables can be declared and assigned values before use. Declare variables by using the VaR keyword. The biggest benefit of declaring variables is the ability to detect errors in the code in a timely manner. Because JavaScript uses dynamic compilation, it is not easy to find errors in the Code, especially the variable naming.
There is also an importance for variables-that is, the scope of the variables. Global variables and local variables are also available in JavaScript. Global variables are defined outside all function bodies, and their scope is the whole function. Local variables are defined within the function body and only visible to this function, other functions are invisible.
Iii. Expressions and operators
1. Expression
After defining variables, you can assign values, change, and compute them. This process is usually called an expression, it can be said that it is a set of variables, constants, Boolean and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, value assignment expressions, and boolean expressions.
2. Operators
A series of symbols used to complete operations by operators. In JavaScript, there are arithmetic operators such as +,-, *, And/. Comparison operators such! =, =, And so on; there are logical boolean operators such! (Reverse), |, |, and other string operations, such as + and + =.
In JavaScript, there are binary operators and single-object operators. The binary operator consists of the following:
Operand 1 operator operand 2
It consists of two operands and an operator. Such as 50 + 40 and "this" + "that. A single-object operator requires only one operand, and its operator can be in front or back.
(1) Arithmetic Operators
Arithmetic Operators in Javascript include the single object operator and binary operator.
Binary operators: + (plus),-(minus), * (multiplication),/(Division), % (Modulo), | (by bit or), & (by bit and), <(left shift),> (right shift), >>> (right shift, zero fill ).
Single Object OPERATOR:-(inverse ),~ (Complement), ++ (increment 1), -- (decrease 1 ).
(2) Comparison Operators
The basic operation process of the comparison operator is to first compare its operands, and then return a value of true or false. There are eight comparison operators:
(3) Boolean logical operators
Added several boolean operators in javascript :! (Reverse), & = (and post-assignment), & (logical and), | = (or post-assignment), | (logical or), ^ = (assign values after an exclusive or exclusive value), ^ (logical or exclusive value ),? (Three-object operator), | (OR), = (equal to), | = (not equal ).
The main format of the Three-object operator is as follows:
Operand? Result 1: result 2
If the result of the operand is true, the result of the expression is result 1, otherwise it is result 2.
Iv. Examples
The following is a JavaScript document on the effect of the running horse.
Test2_1.html
<HTML>
<Head>
<Script language = "JavaScript">
VaR MSG = "welcome to the Shanxi window (www.shanxi?#net) Website ";
VaR integer = 100;
VaR spacelen = 120;
VaR space10 = "";
VaR seq = 0;
Function scroll (){
Len = msg. length;
Window. Status = msg. substring (0, seq + 1 );
SEQ ++;
If (SEQ> = Len ){
SEQ = spacelen;
Window. setTimeout ("scroll2 ();", interval );
}
Else
Window. setTimeout ("scroll ();", interval );
}
Function scroll2 (){
VaR out = "";
For (I = 1; I <= spacelen/space10.length; I ++) Out ++ =
Space10;
Out = out + MSG;
Len = out. length;
Window. Status = out. substring (SEQ, Len );
SEQ ++;
If (SEQ> = Len) {seq = 0 ;};
Window. setTimeout ("scroll2 ();", interval );
}
Scroll ();
</SCRIPT>
<Body>
</Body>
</Html>
This section describes how to add a Javascript script to a web page and describes the basic data types, variables, constants, and Operation operators in JavaScript. It can be seen from the content in this lecture that it is very easy and pleasant for people who have mastered the C ++ language to learn JavaScript.