JavaScript "2"

Source: Internet
Author: User

Condition Judgment:

Use if () {}else{} in JavaScript to determine

If the statement block contains only one statement, you can omit {}:

var age=20;

if (age>=18)

Alert ("You are an adult")

Else

Alert ("You haven't grown up, baby")

Suggested write {}

Multi-line condition judgment: if () {

}else if () {

}else{

}

Cycle:

To calculate the value we can write the expression directly 1+2+3//6

To calculate 1+2+3+4+5+......+10 can barely write it down

But to calculate 1+2+3+.....+10000, it's impossible to write the expression directly.

In order for the computer to compute thousands of repetitive operations, we need to loop the statement:

There are two types of JavaScript loops, one for loops, and the execution of statements by looping, ending conditions, and incrementing conditions:

var x=0;

var i;

for (i=1;i<=10000;i++) {

X=x+1

}

Control conditions for the FOR loop:

I=1 This is the initial value assigns the variable I to a value of 1

i<=10000 This is the judging condition, the loop continues when it is satisfied, and exits when it is not satisfied.

i++ This is the increment condition after each loop, because the variable i is incremented by 1 after each cycle, so he will eventually exit the loop after several cycles without satisfying the condition i<10000

The most common place for a for loop is to iterate through an array using an index:

var arr=["Apple", "Banana", "Orange"]

var i, X;

for (i=0;i<arr.length;i++) {

X=arr[i]

Console.log (x)

}

Shorthand:

var arr=["Apple", "Banana", "Orange"]

for (Var i=0;i<arr.length;i++) {

X=arr[i]

Console.log (x)

}

    

for3 Conditions of the loop can be omitted, if there is no Exit loop judgment condition, you must use break the statement to exit the loop, or the dead loop:

0;for (;;) { // 将无限循环下去    if (x > 100) { break; // 通过if判断来退出循环 } x ++;}

JavaScript "2"

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.