Basic data structure for beginners to fully contact JavaScript

Source: Internet
Author: User
Tags arithmetic arithmetic operators bitwise comparison expression functions variables variable
javascript| Beginners | data | structure

JavaScript, which provides scripting languages, is very similar to C + +, except that it removes easily-generated errors such as pointers in C and provides a powerful class library. For people who already have C + + or C, learning the JavaScript scripting language is a very relaxing thing to do.

  One, the addition of JavaScript code

JavaScript scripts are included in HTML, which becomes part of the HTML document. Combined with the HTML logo, it forms a powerful Internet programming language. You can add JavaScript scripts directly to the document:

<script Language = "JavaScript" >
JavaScript language code;
JavaScript language code;
....
</Script>

Description

By identifying <Script>...</Script> indicating JavaScript Script source code will be placed in the meantime.

by attribute language = "JavaScript" describes what language is used in the identity, and this is the JavaScript language, which represents the language used in JavaScript.

Here's an example of adding JavaScript script to a Web document:

Test2.html

<HTML>
<Head>
<script Language = "JavaScript" >
Document. Write ("Welcome to Web Teaching Network");
Document. Close ();
</Script>
</Head>
</HTML>

Call test2.html in the browser's window, and the "This is Sadie Interactive School" string is displayed.

Description

Document. Write () is the output function of the document object that prints the character or variable value in parentheses to the window; Close () closes the output.

The <Script>...</Script> logo can be put into head> ... </Head> or <Body> ...</body>. Place the JavaScript identity

  II. Basic data types

The JavaScript scripting language, like other languages, has its own basic data types, expressions and arithmetic operators, and the basic frame structure of the program. JavaScript provides four basic data types for handling numbers and text, while variables provide the place where information is stored, and expressions can perform more complex processing.

1. Basic Data type

Four basic data types in javascript: numeric values (integers and real numbers), string literals (characters or values enclosed by "" or "), Boolean (make True or false), and null values. The data in the basic type of JavaScript can be constants or variables. Because JavaScript takes the form of a weak type, a variable or constant of a data does not have to be declared first, but rather the type of its data when used or assigned. Of course, you can declare the type of the data first by automatically stating its data type when you assign a value.

2, constant

Integral type Constants

JavaScript constants are often called literal constants, which are data that cannot be changed. Its integer constants can represent their values using 16, octal, and decimal.

Real constants

A real constant is represented by an integer part plus a decimal part, such as 12.32, 193.98. Scientific or standard methods can be used to represent: 5E7, 4E5, etc.

Boolean value

There are only two states of a Boolean constant: TRUE or false. It is mainly used to describe or represent a state or a flag to illustrate the operation process. It is not the same as C + +, and C + + can represent its state in 1 or 0, while JavaScript can only use TRUE or FALSE to indicate its state.

Character-type constants

Use one or more characters enclosed in single quotes (') or double quotes ("). such as "This are book of JavaScript", "3245", "ewrt234234", and so on.

Null value

There is a null value in JavaScript that means nothing. If you attempt to reference a variable that is not defined, a null value is returned.

Special characters

Like C, JavaScript also has special characters that are not visible at the beginning of the backslash (/). Often called control characters.

3, variable

The main function of variables is to access data and provide containers for storing information. For a variable, you must specify the name of the variable, the type of the variable, the declaration of the variable, and the scope of the variable.

Name of variable

The naming of variables in JavaScript is very similar to their computer language, and here are two points to note:

A, must be a valid variable, that is, the variable begins with a letter, the middle can appear numbers such as Test1, Text2 and so on. The variable name cannot have spaces, (+), (-), (,), or other symbols except for the underscore (-) as a hyphen.

b, you cannot use a keyword in javascript as a variable.

More than 40 class keys are defined in JavaScript, and these keys are used internally by JavaScript and cannot be the name of a variable. such as Var, int, double, true cannot be the name of a variable.

When naming a variable, it is best to match the meaning of the variable with the meaning of its representative, lest there be an error.

Type of variable

In JavaScript, a variable can be declared with the command var:

var mytest;

This example defines a mytest variable. But there is no value assigned to it.

Var mytest= "This is a book"

The example defines a mytest variable and assigns its value.

In JavaScript, a variable can be declared without a declaration, and the type of the variable is based on the type of the data when it is used.

Such as:

x=100
Y= "125"
xy= True
cost=19.5 and so on.

where x is an integer, Y is a string, XY is a Boolean, and the cost is solid.

Declaration of a variable and its scope

JavaScript variables can be declared before they are used and can be assigned a value. Declare a variable by using the var keyword. The greatest benefit of declaring a variable is the ability to detect errors in the code in a timely fashion, because JavaScript is dynamically compiled, and dynamic compilation is not easy to detect errors in code, especially in terms of variable naming.

There is another importance to a variable-that is, the scope of the variable. There are also global variables and local variables in JavaScript. Global variables are defined outside the body of all functions and are scoped to the entire function, while local variables are defined within the function body and are visible only to the function, but not to other functions.
 
  Three, expressions and operators

1. Expression type

After you have defined the variables, you can assign them, a series of operations, such as change, computation, and so on, usually called an expression, that is, a set of variables, constants, Boolean, and operators, so that an expression can be divided into arithmetic expressions, string expressions, assignment expressions, and Boolean expressions.

2, operator

Operator completes a sequence of symbols that have arithmetic operators in JavaScript, such as + 、-、 *,/etc; comparison operators such as!=, = = etc.; There are logical Boolean operators such as! (Take back), | |, | |; There are string operations such as +, + +.

In JavaScript there are mainly binocular operators and monocular operators. Its binocular operators consist of the following:
Operand 1 operator operand 2

That consists of two operands and an operator. such as 50+40, "this" + "that" and so on. The monocular operator, which takes only one operand and its operator can be either before or after.

(1) Arithmetic operators

Arithmetic operators in JavaScript have a single eye operator and a binocular operator.

Binocular operators:

+ (plus),-(minus), * (multiply),/(except),% (modulo), | (bitwise OR), & (Bitwise AND), << (move left), >> (move right), >>> (right shift, 0 padding).

Monocular operator:

-(Take Back), ~ (take up), + + (accumulator 1) 、--(descending 1).

(2) Comparison operators

Comparison operator its basic procedure is to compare its operands before returning a value of true or FALSE, with 8 comparison operators:

< (less than), > (greater than), <= (less than or equal), >= (greater than equal), = = (equals),!= (not equal).

(3) Boolean logical operators

Added several Boolean logical operators to JavaScript:

! (inverse), &= (after Assignment), & (Logical AND), |= (or later assigned), | (logical OR), ^= (assigned after or after), ^ (logical xor),?: (Triple-mesh operator), | | (or), = = (equals), |= (not equal to).

The main format of the three-mesh operator is as follows:

How many operands? Result 1: Results 2

If the result of the operand is true, the result of the expression is result 1, otherwise the result is 2.

  Iv. examples

Below is a JavaScript document with a marquee effect.

Test2_1.html

<script language= "JavaScript" >
var msg= "This is a marquee effect of the 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>

This article describes how JavaScript scripts join Web pages and learn about basic data types, variables, constants, operator operators, and so on in JavaScript languages. As you can see, learning JavaScript is a very relaxing thing for someone who has mastered C + + language.



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.