JS Basic Grammar the next day
1.IF syntax
If's syntax:
If (judging condition) {is generally a Boolean value or a relational expression or logical expression
The code to execute;
}
If (judging condition) {is generally a Boolean value or a relational expression or a logical expression
The code to execute;
}else {
The code to execute;
}
If (judging condition) {is generally a Boolean value or a relational expression or a logical expression
The code to execute;
}else if (judging condition) {
The code to execute;
}
2. Ternary operators
Expression 1 (relational expression or logical expression)? Expression 2: Expression 3;
If expression 1 is established, that is, the result of a true or expression 1 returns True, the value of expression 2 is the value of the entire expression, and if the expression 1 is not established, that is, the result of the solution is false, then expression 3 is the value of the entire expression.
Generally used in the context of two situations.
Var sex=prompt ("Please enter Little Red or Xiao Ming");
Var s=sex== "Xiaoming"? "Boys": "Girls";
Console.log (s);
3.switch-case syntax
Switch (value) {is usually a variable or calculated value
Case value 1:
The code to execute;
Break
Case Value 2:
The code to execute;
break;
Case Value 3:
The code to execute;
break;
Case Value 4:
The code to execute;
break;
...
Default:
The code to execute;
break;
Execution process:
When the program executes to switch, first enter the parentheses, calculate the face value, and then take this fixed value followed by the curly braces in the case of the values to match, if the match succeeds, then executes the code in the corresponding box, encounter break, then jump out of the current swicth-case structure, If the match is unsuccessful, it depends on whether the structure has default, if there is, execute the code in default, encounter break out of the structure, if there is no default, then directly jump out of the swicth-case structure, execute the following code.
It is used to judge the value of multiple conditions.
Note: The data type of the value in the case must be the same as the data type of the value in the switch parenthesis.
4.If If-else Switch Three differences:
If a conditional execution of a statement is a condition
If-else a conditional execution of one or the other statement of one of two cases
Switch conditionally executes a statement in a set of statements in one of several cases
5. Short-circuit operation
True if the value is converted to a Boolean type:
Number 123 ....
Not an empty string "abc" ...
When converting to a Boolean with a value of false:
0,nan, "", null,undefined
&& find the wrong one
If both sides of the operand are not Boolean types, the operand is converted to a Boolean type, and the operand with the value false is returned first.
If both sides are false, returns the first operand, if the first value is wrong, returns the first value directly, and returns the second value if the first value is the correct one.
|| Find the right value
If the first value is true, the first value is output directly
Logical Non!
Returns False if the operand is an object
Returns true if the operand is an empty string
Returns False if the operand is a non-empty string
Returns true if the operand is 0
Returns False if the operand is any of a non-0 value
Returns true if the operand is Null,undefined,nan
6.while syntax
while (loop condition) {
The Loop body (code to execute);
}
Execution process:
When the program runs to the while, first enter the parentheses to determine whether the loop condition is set up, if it is true, then enter the curly braces to execute the inside of the loop body, after execution, again back to the parentheses to determine whether the loop condition is set up, if set up again into the braces to execute inside the loop body, Reciprocating cycle. As long as the loop, there will certainly be a line of code to change the loop condition, if there is no code to change the loop condition, we call such a loop called a dead loop.
while (true) {
Circulation body;
}
Enter the number of classes and then enter the student's score to calculate the average grade and overall score of the class participants.
var num =parseint (Prompt ("Please enter class number"));
var I=1;
var sum=0;
var avg;
while (I<=num) {
var score=+prompt ("Please enter the score of the" +i+ "student");
Sum+=score;
avg=sum/i;
i++;
}
Console.log ("class altogether" +num+ "the student, their total score is" +sum+ ", the average result is" +avg ";
do{
Circulation body;
} while (loop condition)
Execution process:
The program executes to do place, first enters the curly brace executes inside the loop body, then carries on the execution result to reach while the parenthesis, to determine inside the loop condition is established, if establishes, then again enters the brace execution inside the loop body, if does not establish, then jumps out the current Do-while loop.
feature, at least once the loop body is executed, and then the loop condition is judged.
7.for Cycle
for (expression 1; expression 2; expression 3) {
Circulation body;
}
for (Var i=1;i<=100;i++) {
Loop body
}
Execution process:
The program runs to the for place, enters the parentheses, executes the expression 1, declares the variable, assigns the initial value, then executes the expression 2, determines whether the loop condition is formed, if the condition is established, then enters the loop body inside the curly brace execution, the first time loop, does not execute the expression 3, after the first loop execution completes, Execute expression 3, change the loop condition, and then execute the expression 2, determine whether the loop condition is established, if it is established, then execute the loop body again, if not, then jump out of the loop.
In one cycle, the bread has another cycle.
for (Var i=1;i<=10;i++) {
for (Var j=1;j<=10;j++{
Loop body
)
}
100 Cycles
Break and Continue
Break two ways to use:
The first is used in swicth-case to jump out of the current program
The second is used in the loop, usually with the while or for loop, and if with the use, the effect is immediately out of the current loop, the following code is no longer executed.
Also commonly used to jump out of a dead loop.
while (true) {
}
Continue:
is also used in the loop, when encountering the Continue keyword, immediately stop this cycle, and then return to the loop condition, determine whether the loop condition is established, if set up to continue the next cycle, if not, then exit the current cycle.
Only the current loop is closed, and the code behind the continue is no longer executed.
8. Arrays
Methods for declaring arrays
var arr = new Array (); Using constructors to create arrays
var arr = []; Create an array using literal methods
The length of the array: the number of data stored in the Arr.length data
var arr=[11,22,33,44,55,66,77];
arr[0]=11;
arr[1]=22;
arr[2]=33;
...
JS Basic syntax