JavaScript provides scripting language programming that is very similar to C++. It just removes the errors that are easy to occur in C language such as pointers, and provides a powerful class library. For those who already have C++ or C language, learning JavaScript Scripting language is a very relaxed and pleasant thing.
First, the addition of JavaScript code
JavaScript scripts are included in HTML, and they become part of the HTML document. Combined with the HTML tag, they form a function A powerful Internet programming language. You can directly add JavaScript scripts to documents:
script language ="JavaScript">
JavaScript language code;
JavaScript language code;
... .
c/script & gt;
Note:
............... & & & &. Indicates that the source code of the javascript script will be placed in the between it. "Indicate which language is used in the logo, here is the JavaScript language, indicating the language used in JavaScript.
The following is an example of adding JavaScript scripts to a Web document:
Test2.html
html>
/script>
/head>
/html>
When test2.html is called in the browser window, the string "This is an online school" is displayed. See Figure 2. .
Figure 2
Description:
Document. write() is the output function of the document object. Its function is to output the characters or variable values in brackets to the window; document. close() is to close the output.
You can put the script>... mark between head>.. or
.../body>. Place the javascript tag ... between the headers so that it is loaded before the home page and the rest of the code, thereby making the code more powerful; you can place the javascript tag between < body>... /body>between the bodies to create documents dynamically in certain parts.
Second, basic data types
JavaScript scripting language, like other languages, has its own Basic data types, expressions and arithmetic operators, and the basic framework structure of the program. JavaScript provides four basic data types for processing numbers and text, while variables provide a place to store information, and expressions can complete more complex information. Processing.
1, basic data types
There are four basic data types in JavaScript: numerical values (integers and real numbers), string types (characters or values enclosed by "" or '') , Boolean type (expressed as True or False) and null value. Data in the basic types of JavaScript can be constants or variables. Since JavaScript adopts a weakly typed form, a data variable or constant does not need to be declared first. Instead, the type of the data is determined when using or assigning a value. Of course, you can also declare the type of the data first, which automatically indicates the data type when assigning a value.
2, constant
Integer type Constants
JavaScript constants are usually also called literal constants, which are data that cannot be changed. Its integer constants can use hexadecimal, octal and decimal to represent their values.
Real constants
A real constant is represented by an integer part plus a decimal part, such as 12.32, 193.98. It can be expressed using scientific or standard methods: 5E7, 4e5, etc.
Boolean value
Boolean constants have only two states: True or False . It is mainly used to illustrate or represent a state or flag to illustrate the operation process. It is different from C++. C++ can use 1 or 0 to represent its state, while JavaScript can only use True or False to represent its state.
Character constants
Use one or more characters enclosed in single quotes (') or double quotes ("). For example, "This is a book of JavaScript ", "3245", "ewrt234234", etc.
Null value
There is a null value in JavaScript, which means nothing. If you try to reference an undefined variable, a Null value is returned.
Special characters
Same as C language Similarly, JavaScript also has some special characters that cannot be displayed starting with a backslash (/). They are usually called control characters.
The main function of variables is to access data and provide a container for storing information. For variables, the name of the variable, the type of the variable, the declaration of the variable and the scope of the variable must be clear.
Naming of variables
JavaScript The variable naming in is very similar to its computer language. The following two points should be noted here:
A must be a valid variable, that is, the variable starts with a letter, and numbers such as test1, text2, etc. can appear in the middle. Except for underscores ( -) Except for hyphens, variable names cannot have spaces, (+), (-), (,) or other symbols.
B, keywords in JavaScript cannot be used as variables.
There are more than 40 class keys defined in JavaScript. These keys are used internally in JavaScript and cannot be used as names of variables. For example, Var, int, double, and true cannot be used as names of variables.
When naming variables, It is best to match the meaning of the variable with what it represents to avoid errors.
Type of variable
In JavaScript, variables can be declared with the command Var:
var mytest;
This example defines a mytest variable, but does not assign it a value.
Var mytest="This is a book"
This example defines a mytest variable and assigns its value.
In JavaScript, variables do not need to be declared, and the type of the variable is determined based on the type of data when used.
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 type.
The declaration and scope of variables
JavaScript variables can be declared before use and can be assigned values. Variables are declared by using the var keyword. The biggest benefit of declaring variables is that errors in the code can be discovered in time; because JavaScript uses dynamic compilation , and dynamic compilation makes it difficult to find errors in the code, especially in the naming of variables.
There is another importance to variables - that is the scope of the variable. There are also global variables and local variables in JavaScript. Global variables Variables are defined outside all function bodies, and their scope is the entire function; local variables are defined within the function body, and are only visible to the function, but not to other functions.
3. Expressions and operators
1. Expressions
After defining the variables, you can perform a series of operations such as assignment, change, and calculation on them. This process is usually It is called an expression to complete. It can be said that it is a collection of variables, constants, Boolean and operators, so expressions can be divided into arithmetic expressions, string expressions, assignment expressions and Boolean expressions, etc.
2, Operators
Operators are a series of symbols used to complete operations. In JavaScript, there are arithmetic operators, such as +, -, *, /, etc.; there are comparison operators such as !=, ==, etc.; there are Logical Boolean operators such as ! (reverse), |, ||; and string operations such as +, +=, etc.
There are mainly binary operators and unary operators in JavaScript. The binary operators are as follows Composition:
Operand 1 Operator Operand 2
That is composed of two operands and one operator. Such as 50+40, "This" + "that", etc. Unary operators, only An operand is required, and its operator can come before or after.
(1) Arithmetic operator
Arithmetic operators in JavaScript include unary operators and binary operators.
Double Operators:
+ (addition), - (subtraction), * (multiplication), / (division), % (modulo), | (bitwise OR), & (bitwise AND), < (shift left), >>(shift right), >>>(shift right, zero padding).
Unary operator:
-(replace),~(take Complement), ++ (increment by 1), -- (decrement by 1).
(2) Comparison operator
The basic operation process of the comparison operator is to first compare its operands , and then return a true or False value, there are 8 comparison operators:
(less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == ( Equal to), != (not equal to).
(3) Boolean logical operators
Several Boolean logical operators have been added to JavaScript:
! (inversion), &=( and (assignment after AND), & (logical AND), |= (assignment after OR), | (logical OR), ^= (assignment after XOR), ^ (logical XOR), ?: (ternary operator), ||(or), == (equal), |= (not equal).
The main format of the ternary operator is as follows:
Operand? Result 1: Result 2
If the operation If the result of the number is true, the result of the expression is result 1, otherwise it is result 2.
Four, Example
The following is a JavaScript document with a marquee effect.
Test2_1.html
html>
head>
script Language="JavaScript">
var msg="This is a marquee effect JavaScript document";
var interval = 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>