Composition of JavaScript tutorials

Source: Internet
Author: User
The basic structure of JavaScript scripting language is implemented by controlling statements, functions, objects, methods, attributes, and so on. The basic structure of JavaScript scripting language is implemented by controlling statements, functions, objects, methods, attributes, and so on.

Program Control Flow

In any language, the control flow of a program is required. It can reduce the entire program and make it run smoothly in a certain way. The following are the control flow structures and statements commonly used in JavaScript:

1. if Condition Statement

Basic Format:

If (declarative ){

Statement segment 1;

......

} Else {

Statement Segment 2;

.....

}

Function: If the expression is true, statement segment 1 is executed; otherwise, statement Segment 2 is executed.

2. For Loop statements

Basic Format:

For (initialization; condition; increment)

Statement set;

Function: implements a conditional loop. When a condition is set, the statement set is executed. Otherwise, the loop body jumps out.

Description: The initialization parameter indicates the starting position of the loop and the initial value of the variable must be assigned.

Condition: it is a condition used to determine when the loop is stopped. If the conditions are met, the system executes the loop body. Otherwise, the system jumps out.

Increment: mainly defines how the cyclic control variables change during each loop. Separate the three main statements with commas.

3. while Loop

Basic Format:

While (condition)

Statement set;

This statement is the same as the For statement. If the condition is true, the loop is repeated. Otherwise, the loop is exited.

For and while statements

Both statements are cyclic statements, which are easier to understand and compact when using For statements to process related numbers. while statements have more special effects on complex statements.

4. break and continue statements

Similar to the C ++ language, the break statement is used to make the loop jump out of the For or while clause. The continue skips the remaining statements in the loop and enters the next loop.


Function

Functions provide a convenient capability for program designers. During a complex program design, the program is always divided into relatively independent parts based on the functions to be completed, and a function is written for each part. Thus, each part is fully independent, with a single task, clear procedures, easy to understand, easy to read, and easy to maintain. JavaScript Functions can encapsulate modules that may be used multiple times in a program. Programs that can be called as event-driven results. Thus, a function is implemented to associate it with event-driven. This is different from other languages.

1. JavaScript function definition

Function Name (parameter, variable ){

Function body ;.

Return expression;

}

Note: When a function is called, the variables or literal values can be transferred as variable elements.

A Function is defined by the keyword Function.

Function Name: define the name of your function.

A parameter table is a value passed to a function for use or operation. Its value can be a constant, variable, or other expression.

Call a function by specifying the function name (real parameter.

Return must be used.

The function name is case sensitive.

2. Formal parameters in Functions

In the function definition, we can see that there is a parameter table after the function name. These parameter variables may be one or several. How can we determine the number of parameter variables? In JavaScript, you can use arguments. Length to check the number of parameters.

Example:

Function function_Name (exp1, exp2, exp3, exp4)

Number = function _ Name. arguments. length;

If (Number> 1)

Document. wrile (exp2 );

If (Number> 2)

Document. write (exp3 );

If (Number> 3)

Document. write (exp4 );

...

The above is the content of the JavaScript tutorial program. For more information, see the PHP Chinese website (www.php1.cn )!

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.