Part III: Process Control statements
The JavaScript code is the writing location:
JavaScript code should be written in <script type="Text/javascript" ></script> this pair of tags.
or as an external reference <script src= "JavaScript code path " ></script>
each line of JavaScript code is terminated with a semicolon.
Output statement
1, console output:console.log ();
It can output some information in the console, and the output information is the contents of the parentheses in Console.log () .
This statement is commonly used when debugging a program.
2. Popup Box output:
alert ();
You can use alert to pop up a prompt on a webpage to display the information in the parentheses of alert () .
Prompt ();
using prompt , you can pop up an input box on a Web page and use the information in the parentheses of prompt () as a cue message.
Confirm ();
Contains the OK and cancel pop-up boxes.
3, page output:document.write ();
The content is displayed directly on the page.
SELECT statement
If....else Statements
if ( judging condition /boolean value ){
The following code is executed when the condition is met
code 1;
}Else {
when the above condition is not satisfied, or the boolean value is false , the following code is executed 2
Code 2
}
In addition,else you can continue to add if judgment
if ( judging condition /boolean value ){
The following code is executed when the condition is met
code 1;
}else if ( judging condition ) {
when the above condition is not satisfied, or the boolean value is false , the following code is executed 2
Code 2
}
else if ( judging condition )
。。。。。
else {
Final code
}
Switch ... case statement
Used to determine multiple values that may appear
The switch statement is most closely related to the IF statement and is also a flow control statement commonly used in other languages.
switch (expression) {
Case Value:statement
Break
Case Value:statement
Break
Case Value:statement
Break
Case Value:statement
Break
Default:statement
}
Switcheach of the scenarios in the statement ( Case) means:"If an expression equals this value (value), the following statement is executed (Statement)". and Breakkeyword causes code execution flow to jump outSwitchstatement. If omitted Breakkeyword, it will result in the execution of the current Case, proceed to the next Case. by for each Caseafter that, add a Breakstatement, you can avoid executing multiple Casethe case of the code.
can also be mixed in a variety of situations.
switch (i) {
Case 25:
/* Merge two Scenarios * /
Case 35:
Alert ("or 35");
Break
Case 45:
Alert ("45");
Break
Default
Alert ("other");
}
It is important to note that the switch statement uses the strict equality operator when comparing values, so type conversions do not occur (for example,
The string "Ten" is not equal to the value of ten).
break and continue statements
Break is the end of the qualifying loop inside the loop.
Continue is to end the loop inside the loop and start the next loop:
Looping statements
The For loop is the same piece of code that executes repeatedly.
for (var i = 1; Judging conditions ; i++) {
Block of code to loop:
}
when the program runs to for, it declares a variable iand assigns a value of 1todetermine i Whether to satisfy the following judgment condition, if satisfied, execute below to loop the code block, the code block executes after executing i++, and then judge whether the condition is satisfied, if meet again according to the above process execution, if not satisfied, end for loops.
The for loop can also be used for nesting, implementing complex operations, and bubbling sorting uses a for loop nesting. Below is an example of two for loop nesting.
Print Right triangle,
for (var i = 1; i <=; i++) {
for (var j = 1; J <= I; j + +) {
document.write ("☆");
}
document.write ("<br/>");
}
print The multiplication table
for (var i = 1; i < i++) {
for (var j = 1; J <= I; j + +) {
document.write (j + "*" + i + "=" + i * j);//1 * 1 = 1
document.write ("");
}
document.write ("<br/>");
}
For in Loop
The for-in is used to iterate over the properties of an array or an object (looping over a group or an object's properties).
As an example,
var x
var mycars = new Array ()
Mycars[0] = "Saab"
MYCARS[1] = "Volvo"
MYCARS[2] = "BMW"
for (x in Mycars)
{
document.write (Mycars[x] + "<br/>")
}
While Loop
while ( judging condition /boolean) {
code block
}
When the current code executes to the while, it will first determine if the condition is true, and if true, then execute while block of code in curly braces, after execution of the code block, return to the while again to judge, if true, again to execute the code block in the while brace, and then back to thewhile, if false is not performed.
Note: In the future when writing code must pay attention to the loop judgment condition can not always be true, otherwise it will become a dead loop.
Do...while Cycle
The Do-while statement is a post-test looping statement that tests export conditions only after the code in the loop body executes.
in other words, the code in the loop body is executed at least once before the conditional expression is evaluated.
do {
Statement
} while (expression);
Do...while statements are not used in development, most are used for loops, and for loop nesting.
Add:
Function Object
function declaration Type
function fn () {
function Body
}
function Expression ( anonymous function )
var fn = function () {
function Body
}
FN indicates the function name
function expressions are often called anonymous functions because there is no function name
Call to function
FN ();// Note : The function only declares that no call will be performed
parameters of the function
affirm
function function name ( parameter 1, parameter 2, parameter 3,) {
function Body
}
called
Name of function ( argument 1, argument 2, argument 3);
Name of function ( argument 1);// There's no problem with that writing.
Name of function ( argument 1, argument 2, argument 3, actual parameter 4);// There's no problem with that writing.
Note : The number of arguments for a function can be different from the number of formal parameters
return value of the function
function function name () {
return the value to be returned ;
}
Note : The default return value when the function does not write the return value is undefined
recursive function: function calls itself function internally called recursion
function fn () {
FN ();
}
FN ();
Callback for function : a function that is passed as a parameter is called a callback function.
function fn1 () {
Console.log (" I am the callback function ");
}
function fn2 (parameter) {
Parameter (); calling Functions
the parameter here is the function that the formal parameter represents in the fn1
}
FN2 (FN1);//Fn1 is a callback function
JavaScript-based Process Control statements