Understanding the usage of for, while and recursive in JavaScript basics

Source: Internet
Author: User

For loop:

Copy Code code as follows:

For (I=start i<end; i++) {

}


While loop: (Note that if the condition is always true, it goes into the dead loop and the browser hang off)
Copy Code code as follows:

while (condition) {
Do something;
Change condition;
}

recursion:

Use for loop to do substring
Copy Code code as follows:

function substring (all, start, end) {
For (I=start i<=end; i++) {
Console.log (All[i]);
}

SUBSTRING ("Eclipse", 1, 4); Clip


using recursion to implement substring
Copy Code code as follows:

function substring (all, start, end) {
if (start >= end) {
return All[start];
}
else {
return All[start] + substring (all, start+1, end);
}

SUBSTRING ("Eclipse", 1, 4); Clip


To Access object properties using a For loop:

For arrays, strings, we use index [] to access a particular value; For objects, it's the same as using [], but we'll use a special variable: propertyname

Copy Code code as follows:

var person = {
Name: "Morgan Jones",
Telephone: "(650) 777-7777",
Email: "Morgan.jones@example.com"
};

For (Var propertyname in person) {
Console.log (PropertyName + ":" + person[propertyname]);
}


use a For loop to find the data in the array:
Copy Code code as follows:

var table = [
[Person ', ' age ', ' city '],
["Sue", "San Francisco"],
["Joe", "Halifax"]
];

var i;
var rows=table.length;
for (r=0;r<rows;r++) {
var C;
var cells = table[r].length;
var rowtext = "";
for (c=0;c<cells;c++) {
Rowtext + = Table[r][c];
if (c < cells-1) {
Rowtext + = "";
}
}
Console.log (Rowtext);
}


results:
Person Age City

Sue San Francisco

Joe Halifax

--------------------------------------------------------------------------------

Break

Use the break to exit the loop immediately, which applies to the for and while loops.

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.