JavaScript Foundation Hardening 3--Syntax 2

Source: Internet
Author: User

2.1 JavaScript Statement 2.1.1 If/else statement

The If/else in JavaScript determines the choice you will find, just like the C language. The syntax format is this:

if (Condition 1)  {  true  when executing the code;  } Else if (Condition 2)  {  true  when executing the code;  } Else   {  true  when executing the code;  }
2.1.2 Switch/case Statements

When you do a lot of choice, if you still use the IF/ELSE structure, then the code can become messy, so we use the switch/case structure:

Switch (k) { case K1:  1 ;    Break ;  case K2:  2 ;    Break ; default:    match found in case);}
2.1.3 For Loop

The For loop is a tool that programmers often use for entry, and in JavaScript, the syntax for a for loop is very similar to the C language, in the format:

 for (variable = initial value; loop condition; variable accumulation method) {loop statement;}

For example, make it clearer, for example, to cycle through the numbers that print out 0~7:

for (var i=0;i<8;i++) { Document. Write ("number is" +i+ "<br>");} </script></body>
2.1.4 While loop

Another way of looping apart from A For loop:

 while (condition)  {  The code that needs to be executed;  }
In addition, the while loop:
 Do   {  The code that needs to be executed;  }  while (conditions);
The difference between the two is thatdo/whilefalse,the Do /while also executes the loop code once.
2.1.5 Break and Continue statements

Sometimes in the loop, you need to jump out of the loop or skip the rest of the code in the loop to make the next loop, which is the function of break and continue.

    • Break this statement in the loop body, the role is immediately jump out of the loop.

    • Continue this statement in the loop body, the role is to abort this cycle, and perform the next cycle. If the condition of the loop has not been met, jump out of the loop.

Like what:

 for (i = 1; i < i++  ) {ifcontinue  ; if  Break ;  Document. write (i);}

The output is "12347", that is, skipping 5 and 6, and then jumping out of the loop when i==8.

2.2 JavaScript functions

In a complex programming process, you need to divide the program into relatively separate parts, each writing a "function", according to the function you want to accomplish. Thus, the parts are fully independent, the task is single, the procedure is clear, easy to read and easy to maintain.

JavaScript functions can encapsulate a program that may be used multiple times in a program, and can be invoked as an event-driven result, to implement a function that is associated with event-driven, which is different from other languages.

In JavaScript, functions are defined by the keyword function, which can have multiple parameters. The basic format is:

function function name (parameter 1, parameter 2) {  function body;   return return value;}

When calling (using) a function, it is possible to implement various complex functions by passing in the corresponding parameters and executing various statements such as if/else,switch/case,for,while in the function body.

Duplicate declaration of a function: If you repeatedly declare the same function using the function command more than once, the following declaration overrides the preceding declaration, such as this code:

<script>function  my_func () {document. Write (1);} My_func () document. Write ("<br>"); function My_func () {  document. Write (2);} My_func ()</script>

JavaScript Foundation Hardening 3--Syntax 2

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.