JavaScript Learning Notes of the statement _ Basic knowledge

Source: Internet
Author: User

One, Conditional branch statement : if

Basic format:

if (< expression 1>) {
< statement Group 1>
}else if (< expression 2>) {
< statement Group 2>
}else{
< statement Group 3>
}

Execution process:

Second, the circular statement

2.1 Pre-Test Loop statement: Evaluate the export condition before the code in the loop body is executed.

2.1.1while statement

Basic format:

do {
< statement group >
while (< expression >)

Execution process:


2.1.2 For statement

Basic format:
for (< initial expression >;< conditional expression >;< Change expression >) {
< statement group >
}

Execution process:


2.2 Test Loop statement: The export condition is not tested until the code in the loop body is executed.

2.2.1, do-while statements

Basic format:

do {
< statement group >
while (< expression >);

Execution process:

third, accurate iterative statement : for-in

Basic format:
For (property in object) {
< statement group >
}

Function: Repeats all properties of the specified object, which can be used to enumerate the properties of the object.

Example:

Copy Code code as follows:

<body>
<p> Click on the button below to iterate through the properties of the object "person". </p>
<button onclick= "MyFunction ()" > click here </button>
<p id= "Demo" ></p>
<script>
function MyFunction ()
{
var x;
var txt= "";
var person={fname: "Bill", lname: "Gates", age:56};
for (x in person)
{
Txt=txt + person[x];
}
document.getElementById ("Demo"). Innerhtml=txt;
}
</script>
</body>

If the object to be represented is null or undefined, the loop body will no longer execute or throw an error, so you should first detect if the property value of the modified object is null or undefined when you perform the for-in loop.

Iv.lable Statements

Basic syntax:

such as: Begin:for (var i = 0; i < i++) {
alert (i);
}
Examples illustrate the role of lable statements:
Not added lable:

Copy Code code as follows:

var num = 0;
for (var i = 0; i < i++) {
for (var j = 0; J < J + +) {
if (i = = 5 && J = = 5) {
Break
}
num++;
}
}
alert (num);//95

Join lable:

Copy Code code as follows:

var num = 0;
Outpoint:
for (var i = 0; i < i++) {
for (var j = 0; J < J + +) {
if (i = = 5 && J = = 5) {
Break outpoint;
}
num++;
}
}
alert (num); 55

The first example output 95 is not difficult to understand, the second example why output 55, because the execution to break outpoint, jump directly to the Putpoint layer, execute alert statement.

If you change the second example to the following:

Copy Code code as follows:

var num = 0;
for (var i = 0; i < i++) {
Outpoint:
for (var j = 0; J < J + +) {
if (i = = 5 && J = = 5) {
Break outpoint;
}
num++;
}
};
alert (num);//95

This result is consistent with the result of the first example.

V. Break and Continue statements

5.1break statement:

Copy Code code as follows:

var num = 0;
for (var i = 1; i < i++) {
if (i%5==0) {
Break
}
num++;
};
alert (num);//4

Jump to alert statement after execution of break statement

5.2continue statement:

Copy Code code as follows:

var num = 0;
for (var i = 1; i < i++) {
if (i%5==0) {
Continue
}
num++;
};
alert (num);//8

The continue statement jumps to the for () loop and continues to execute the loop until the loop condition is not valid.

Six, with statement

Basic syntax:

With (object) {
Statements
}
An example is provided:
Do not use with:

Copy Code code as follows:

var qs = location.search.substring (1);
var hostName = Location.hostname;
var url = location.href;
Alert (QS);
alert (hostName);
alert (URL);

Use with To:

Copy Code code as follows:

With (location) {
var qs = search.substring (1);
var hostName = HostName;
var url = href;
}
Alert (QS);
alert (hostName);
alert (URL);

As you can see from the example above, the action of the WITH statement is to set the scope of the code to a specific object, reducing the repeated input.

However, the JS interpreter needs to check whether the variable in the With block belongs to the with contained object, which will cause the WITH statement execution speed to drop greatly, and cause the JS statement to be difficult to be optimized.

It is therefore not recommended to use the WITH statement on a large scale.

Vii.. Swith statements

Basic syntax:
Switch (< expression >) {
Case < numerical 1>:< Statement group 1>
Break
Case < numerical 2>:< Statement group 2>
Break
...
Default < Statement group >
}

Execution process:

A switch statement can be any data type, and the value of each case is not necessarily a constant, it can be a variable, an expression, and so on, for example:

Copy Code code as follows:

switch ("Hello World") {
Case "Hello" + "World":
Alert ("Greeting was found.");
Break
Case "Goodbye":
Alert ("Closing was found.");
Break
Default
Alert ("Unexpected message is found.");
}

Copy Code code as follows:

var num = 25;
Switch (TRUE) {
Case Num < 0:
Alert ("less than 0.");
Break
Case num >= 0 && num <= 10:
Alert ("Between 0 and 10.");
Break
Case num > && num <= 20:
Alert ("Between 20");
Break
Default
Alert ("More than 20");
}

The switch statement uses the equality operator comparison when compared, so no type conversions occur.

Practice:

Copy Code code as follows:

<script type= "Text/javascript" >
var count = 10;
for (Var i=0 i < count; i++) {
alert (i);
}
alert (i); Output what?
</script>

for (;;) {
Alert ("2");//Output How many times 2?
}

The above is the entire content of this article, I hope that the small partners can help.

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.