JavaScript provides scripting language programming that is very similar to C++. It just removes the errors that are easy to occur in the C language such as pointers and provides a powerful class library. For those who already know C++ or C language, learning JavaScript scripting language is a very easy and enjoyable thing.
Inclusion of JavaScript code
JavaScript scripts are included in HTML and become part of the HTML document. Combined with HTML tags, it forms a powerful Internet programming language. You can add JavaScript scripts directly to the document:
JavaScript language code;
JavaScript language code;
....
Instructions:
Indicate that the JavaScript script source code will be placed between the tags <Script>...</Script>.
Use the attribute Language = "JavaScript" to 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 a JavaScript script to a web document:
<html> <head> <meta charset="utf-8"> <script language ="JavaScript">document.write("这是学吧网!");document.close(); </script> </head> </html>
When the page is called in the browser window, the string "This is Xueba.com!" will be displayed.
Basic data types of JavaScript
JavaScript scripting language, like other languages, has its own basic data types, expressions and arithmetic operators, as well as 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 type (characters or numerical values enclosed with "" or ''), Boolean type (make True or Fals e means) and a null value. The data in JavaScript's basic types can be constants or variables. Since JavaScript adopts a weakly typed form, a data variable or constant does not need to be declared first, but the type of its data is determined when it is used or assigned. Of course, you can also declare the type of the data first, which will automatically indicate its data type when assigning a value.
2. Constants
Integer constants
JavaScript constants are usually also called literal constants, which are data that cannot be changed. Its integer constant can express its value in hexadecimal, octal and decimal notation.
Real type constants
Real type constants are represented by the integer part plus the decimal part, such as 12.32, 193.98. 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 describe or represent a state or sign to illustrate the operation process. It is different from C++. C++ can use 1 or 0 to express its status, while JavaScript can only use True or False to express its status.
Character constants
Use one or several characters enclosed in single quotes ( ' ) or double quotes ( " ). Such as "This is a bookof JavaScript ", "3245", "ewrt234234", etc.
Null value
There is an empty value null in JavaScript, which means there is nothing. If you try to reference an undefined variable, a Null value will be returned.
Special characters
Like the C language, JavaScript also starts with a backslash ( /) Special characters that cannot be displayed. Usually called control characters.
3. Variables The main function of variables is to access data and provide a container for storing information. The name of the variable must be clear. The type, variable declaration and variable scope.
Variable naming
Variable naming in JavaScript is very similar to its computer language. Pay attention to the following two points here:
A. It 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 the underscore (-) as a hyphen, the variable name cannot have spaces, (+), (-), (, ) or other symbols.
B , Keywords in JavaScript cannot be used as variables. There are more than 40 class keywords defined in JavaScript. These keys are used internally by JavaScript and cannot be used as names of variables. For example, Var, int, double, and true cannot be used as names of variables. .
Types of variables
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.
Declaration of variables and their scope
JavaScript variables can be declared before use and can be assigned a value. By using the var keyword Declare variables. The biggest advantage of declaring variables is that errors in the code can be discovered in time; because JavaScript is compiled dynamically, it is difficult to find errors in the code during dynamic compilation, especially in the naming of variables.
There is another importance for variables─that is the scope of the variable. There are also global variables and local variables in JavaScript. Global 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.
JavaScript expressions and operators
1. Expressions
After defining variables, you can perform a series of operations such as assignment, change, and calculation on them. This process is usually called a process. It can be completed by expressions, which can be said to be a collection of variables, constants, Boolean and operators. Therefore, expressions can be divided into arithmetic expressions, string expressions, assignment expressions and Boolean expressions, etc.
2. Operators
Operators are a series of symbols that complete operations. In JavaScript, there are arithmetic operators, such as +, -, *, /, etc.; there are comparison operators such as! =, ==, etc.; there are logical Boolean operators Operators such as ! (Negation) , |, ||; There are string operations such as +, +=, etc.
Arithmetic operators
The arithmetic operators in JavaScript include unary operators and binary operators.
Binary operators: + (addition), - (subtraction), * (multiplication), / (division), % (modulo), | (bitwise OR), & (bitwise AND), << ; (shift left), >> (shift right), >>> (shift right, zero padding).
Unary operators: - (negation), ~ (complement), ++ (increment by 1), -- (decrement by 1).
Comparison Operator
Comparison Operator Its basic operation process 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).
Boolean logical operators
Several Boolean logical operators have been added to JavaScript: ! (negation), &= (assignment after AND), & (logical AND), |= (or assignment after), | ( 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 result of the operand is true, the result of the expression is result 1, otherwise it is result 2.
The above is the content of the basic data structure of JavaScript tutorial. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!