Home > Web Front-end > JS Tutorial > JavaScript study notes statements_basic knowledge

JavaScript study notes statements_basic knowledge

WBOY
Release: 2016-05-16 16:20:14
Original
1082 people have browsed it

1. Conditional branch statement : if

Basic format:

if (){

}else if (){

}else{

}

Execution process:

2. Loop statement

2.1 Pre-test loop statements: The exit condition is evaluated before the code in the loop body is executed.

2.1.1while statement

Basic format:

do {

} while ()

Execution process:


2.1.2 for statement

Basic format:
for (;;){

}

Execution process:


2.2 Post-test loop statement: The exit condition will be tested after the code in the loop body is executed.

2.2.1, do-while statement

Basic format:

do {

} while () ;

Execution process:

3. Accurate iteration statement : for-in

Basic format:
for (property in object){

}

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

Example:

Copy code The code is as follows:



Click the button below to loop through the properties of the object "person".




<script><br> function myFunction()<br> {<br> var x;<br> var txt="";<br> var person={fname:"Bill",lname:"Gates",age:56}; <br> for (x in person)<br> {<br> txt=txt person[x];<br> }<br> document.getElementById("demo").innerHTML=txt;<br> }<br> </script>


If the object to be represented is null or undefined, the loop body will no longer be executed, or an error will be thrown. Therefore, when executing the for-in loop, you should first check whether the attribute value of the object is null or undefined.

4. Lable statement

Basic syntax:

Label:

For example: begin: for (var i = 0; i < 10 ; i ){
alert(i);
                 }
Give an example to illustrate the function of label statement:
Not labelable:

Copy code The code is as follows:

var num = 0;
for (var i = 0 ; i < 10 ; i ){
for (var j = 0; j < 10; j ){
If( i == 5 && j == 5 ){
                          break;
                 }
              num ;
            }
}
alert(num);//95

Join lable:

Copy code The code is as follows:

var num = 0;
outPoint:
for (var i = 0 ; i < 10 ; i ){
for (var j = 0; j < 10; j ){
If( i == 5 && j == 5 ){
break outPoint;
              }
          num ;
         }
}
alert(num); //55

It is not difficult to understand that the first example outputs 95, but why the second example outputs 55 is because when the break outpoint is reached, it jumps directly to the putpoint layer and executes the alert statement.

If you change the second example to the following:

Copy code The code is as follows:

              var num = 0;
for (var i = 0 ; i < 10 ; i ){
outPoint:
for (var j = 0 ; j < 10 ; j ){
If( i == 5 && j == 5 ){
                                                                                                                                                                                                                                                                                                        .                                                                                                        }                                                                                                      }
                  };
alert(num);//95



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

5. break and continue statements

5.1break statement:

Copy code The code is as follows: var num = 0;
for (var i = 1 ; i < 10 ; i ){
If(i%5==0){
                 break;
            }
            num ;
        };
alert(num);//4



After the break statement is executed, jump to the alert statement
5.2continue statement:

Copy code The code is as follows: var num = 0;
for (var i = 1 ; i < 10 ; i ){
If(i%5==0){
Continue;
}
        num ;
};
alert(num);//8



After the continue statement is executed, it jumps to the for() loop and continues to execute the loop until the loop condition is not established.

6. with statement

Basic syntax:

with (object) {

Statements

}

Example:
Writing without using with:


Copy code The code is as follows:

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

Use the writing method with:

Copy code The code is as follows:

with(location){
            var qs = search.substring(1);
            var hostName = hostname;                                                     var url = href;
         }
alert(qs);
alert(hostName);
alert(url);

As can be seen from the above example: the function of the with statement is to set the scope of the code to a specific object and reduce repeated input.

But the js interpreter needs to check whether the variables in the with block belong to the object contained in with, which will greatly slow down the execution speed of the with statement and make the js statement difficult to optimize.

Therefore, it is not recommended to use the with statement on a large scale.

7. switch statement

Basic syntax:

switch () {
case :
                  break;
case :
                  break;
...
default
}

Execution process:

The switch statement can be of any data type, and the value of each case is not necessarily a constant, but can also be a variable, expression, etc., for example:

Copy code The code is as follows:
switch ("hello world") {
case "hello" " world":
alert("Greeting was found.");
                  break;
case "goodbye":
alert("Closing was found.");
                  break;
                    default:
alert("Unexpected message was found.");
         }

Copy code The code is 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 > 10 && num <= 20:
alert("Between 10 and 20.");
                  break;
                    default:
alert("More than 20.");
         }

The switch statement uses the equality operator for comparison, so no type conversion will occur.

Exercise:

Copy code The code is as follows:


for(;;){
alert("2");//How many times is 2 output?
}

The above is the entire content of this article, I hope it can be helpful to my friends.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template