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"