JS Basic Learning 3

Source: Internet
Author: User

1. Control statements

(1) If control statement

If-else basic format if (expression) {statement 1; ...} else{statement 2; ...} Function description if the value of the expression is true Then statement 1 is executed, otherwise the statement 2 is executed

if (0>2) {
Console.log ("success!")
}else{
Console.log ("Failure")
}
>>
Failure

 

(2) switch Select control statement

Switch basic format switch (expression) {case    value 1: statement 1;break;    Case value 2: statement 2;break;    Case Value 3: statement 3;break;    Default: statement 4;}

x=2;
Switch (x) {
Case 0:console.log ("Sunday");
Case 1:console.log ("Monday");
Case 2:console.log ("Tuesday");
Case 3:console.log ("Wednesday");
Case 4:console.log ("Thursday");
Case 5:console.log ("Friday");
Case 6:console.log ("Saturday");
Default:console.log ("undefined");
}
>>
Tuesday

  

(3) For loop control statement

For loop basic format for (initialize; condition; increment) {statement 1; ...} The function description realizes the condition loop, when the condition is established, executes the statement 1, otherwise jumps out the loop body

for (Var i=0;i<10;i++) {
document.write ("}
>>
Hello0hello1hello2hello3hello4hello5hello6hello7hello8hello9

  

(4) While loop control statement

While loop basic format while (condition) {statement 1; ...} Function description run function and for similar, when the condition is formed loop execution statement curly braces {} Inside the statement, otherwise jump out of the loop

var i=0;
while (I<10) {
document.write (" i++;
}
>>
Hello0hello1hello2hello3hello4hello5hello6hello7hello8hello9

  

2. Exception handling

try {    //This piece of code runs from top to bottom, where any one of the statements throws an exception the code block ends running}catch (e) {    ///If an exception is thrown in a try code block, the code in the catch code block is executed.    //e is a local variable that is used to point to an Error object or other thrown object}finally {//Whether or not the code in the     try is thrown (even if there is a return statement in the try code block), the finally code block is always executed. }

(1) Normally, no exception is run        try {            console.log ("Hello") that does not trigger the catch;        }        catch (e) {            console.log (e);        }        finally {            Console.log ("finally");        } >>hellofinally (2) An exception is detected when an unknown element x appears in the following code, and the run of the catch is triggered after the try has run out of print hello.        try {            console.log ("Hello");                Console.log (x);        }        catch (e) {            console.log (e);        }        finally {            Console.log ("finally");        } >>HELLOREFERENCEERROR:X is not definedfinally

  

JS Basic Learning 3

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.