The operator in JS
1. Arithmetic operation (single-mesh operator)
+ Plus, minus, * multiply,/divide,% remainder, + + increment operator 、--The self-decrement operator;
>>>+: There are two kinds of functions, link string/addition operator. When the + sides are all numbers, the addition operation is performed, and when the + sides have any side as a string, the function of the link string, and the result after the link is the word + character string. In addition to +, the rest of the symbolic operation, will first try to use the number function to convert the left and right variables into numbers;
>>>/: The result will retain the decimal point.
>>>++: The increment operator, the variable is based on the original +1;
--: The self-decrement operator, the variable on the original basis-1;
[Similarities and differences of a++ and ++a]
① the same point: regardless of the a++ or ++a, after the operation, the value of a will be +1;
② no power: a++, first with a value to calculate, then put a+1;
++a, first put a+1, and then use the value of a+1 later to calculate;
eg
So, a=5,b=5,c=7;
2. Assignment operation
= Assignment Number (assigns the right value to the left); + = = *=/=%=
+=:a+=b; the equivalent of a=a+b; however, the former is faster than the latter, so it is recommended to use + = notation;
eg
3. Relational operations
= = = (equals), = = = (strictly equals),! = (not equal to),! = = (not congruent/not strictly equal to) >, <, >=, <=
>>> relational operator, the result after the operation, can only be a Boolean type (TRUE or false);
>>> to determine if a number is in a certain range, you must use && link
eg
var a=3;
Alert (a<10 && a>2); √
10>a>0x
>>> = = = (strictly equal to: not only the same value, the type must be the same; different type, the result is false; the type is the same, then the next judgment;)
= = (equals. The same type as the = = = effect. Type is not the same, the first attempt is to use the number function to convert both sides to numbers before judging. But there are individual exceptions, such as null==falsexnull==undefined√)
4, conditional operator (multi-mesh operator)
a>b?true:false The
has two important symbols:? And:
When? When the preceding part evaluates to True, execute: The preceding code;
When? When the preceding part evaluates to False, the following code is executed:
The colon can be a numeric value, and the entire formula can be used for assignment. var a = 1<2?1:2;
The colon can be a code block on either side, and the code will be executed directly. 1<2?alert (1): Alert (2); The
Multi-mesh operator can be nested in multiple layers. var a = 1<2?alert (1):(1>0?4:5);
&& (with, and), | | (OR),! (non)
&& Both sides are true
| | Both sides are set up with the result of true
&& and | | Simultaneous presence, first count && (&& high priority)
priority level
7. Example
① Judging the number of daffodils
Operation Result:
② Arithmetic Device
Two, the branch structure in JS
[If-else structure]
1, the structure of the wording:
if (judging condition) {
When the condition is true, execute if {}
}else{
When the condition is false, execute else's {}
}
2. Precautions:
The ①else{} statement block can be omitted as appropriate.
{} After ②if and else can be omitted, but after {} is omitted, if and else are followed by only one statement; (therefore, it is not recommended to omit {})
3. If the judgment condition in the (), support the situation:
①boolean:true is True, False is false;
②string: The empty string is false, and all non-empty strings are true;
③number:0 is false, all non-0 figures are false;
④null/undefined/nan: All is false;
⑤object: All is true;
[Multiple if, ladder if structure]
1, the structure of the wording:
if (condition i) {
When conditions are established, the actions performed
}else if (condition two) {
Condition one not established && condition II established, operation performed
}else{
When all of the above conditions are not true, the action performed
}
2, multiple if structure, each judgment condition is mutually exclusive, execution chooses one of the road execution. After encountering the correct option and executing, it jumps out of the structure and no longer determines the subsequent branch;
[Nested IF structure]
1, the structure of the wording:
2. In a nested if structure, if you omit {}, the else struct will always belong to the nearest if structure.
3, nested structure can be nested multiple layers, but generally not recommended more than 3 layers. Nested if is generally not recommended for use with multiple if structures.
Getting Started with JavaScript (ii)