Javascript tutorial-from entry to entry (2)

Source: Internet
Author: User

Basic JavaScript Data Structure

The programming of the scripting language provided by JavaScript is very similar to that provided by C ++. It only removes common errors such as pointers 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:
Identify <SCRIPT>... </SCRIPT> to indicate that the Javascript script source code will be placed in it.
The attribute Language = "JavaScript" is used to describe the language used in the identification. Here, it is the Javascript language, indicating the language used in JavaScript.

The following is an example of adding a Javascript script to the Web document:
Test2.html

<HTML>
<Head>
<Script language = "JavaScript">
Document. Write ("this is SCID interactive School ");
Document. Close ();
</SCRIPT>
</Head>
</Html>

When test2.html is used in the browser window, the "this is the interactive School" string is displayed. See figure 2.

Figure 2
Note:
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 place the <SCRIPT>... </SCRIPT> identifier between head>...

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 Types
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
Integer constant
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.
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.
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.
Constant
One or several characters enclosed by single or double quotation marks. Such as "this is a book of JavaScript", "3245", and "ewrt234234.
Null Value
Javascript has a null value, indicating nothing. If you try to reference a variable without definition, a null value is returned.
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.
Variable name
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. Javascript keywords cannot be used 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.
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.
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:
-(Reverse ),~ (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:
<(Less than),> (greater than), <= (less than or equal to),> = (greater than or equal to), = (equal ),! = (Not equal ).
(3) Boolean logical operators
Several boolean operators are added to 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 = "This Is A JavaScript document for the running horse effect ";
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 article 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 that for those who have mastered the C ++ language, it is really easy to learn JavaScript.

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/likaier/archive/2005/03/24/328788.aspx

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.