The operators in JS can be broadly divided into 4 classes: 1 arithmetic operators. 21-dollar operator. 3 comparison operator. 4 logical operators.
Arithmetic operators generally refer to the five operators of subtraction: +,-,*,/,%. The variables in JS can be manipulated by arithmetic operators. Such as:
var a=100,b,c,d,e,f; b= a+10; c=a-10; d=a*10; E=A/10; Ten f=a%3; 1
A unary operator refers to an operator that can manipulate only one value, such as I++,i--, ++i,--i;
Here to pay attention to the difference between i++ and ++i;
Vara=10,b=10,c,d;c= (a++) +2;//c=12d= (++a) +2;//d=13
From the above example, we can see that i++ is the first to participate in the operation, and then add one. And the ++i is I first self plus repeated participation in the next calculation.
The comparison operator is the <, >=, <=, = =,!=,===,!== these are the operators used to compare the size or equality of two data.
var i = 100;var n = 100;alert (i = = n);//outputs True;alert (i! = n);//outputs False;alert (i = = = N)
Note Here is the difference between = = and = = =, as long as the comparison of the two values are equal to true, the values of different data types can be compared according to the provisions in JS. = = = requires both data type and value to be equal to true.
logical operator &&, | | , !。
The value after the operation of the logical operator is a Boolean value, which is important in the conditional statement.
var i = 8; Alert (i<5 && i<10),//outputs false alert (i > | | i < TEN); Outputs True alert (! ( (> 5));//outputs false
It is important to understand the precedence of operators in the equations in which the logical operators participate.
Typically, arithmetic operators > Comparison operators > Logical operators > Assignment operators (=).
In the logical operator,! >| | >&&;
The If statement plays a very important role as the conditional judgment statement in JS.
Its syntax is: if (condition) Statements1 else statement2
var goal=40+parseint (60*math.random ()); if (goal>=80) { document.write (' Excellent score: ' +goal+ ' points! ") }else if (goal>=60) { document.write (" Score qualified: "+goal); } else{ document.write ("Score Unqualified:" +goal+ ") }
In the example above, a random number of 40 to 100 is set, and the size of the value is judged by the IF statement, which is divided into three ranges.
Switch is also a conditional judgment statement whose syntax is
switch (expression) {
Case Value:
Statement
Break
Case Value:
Statement
Break
Default
Statement
}
var arr=["A", "B", "C", "D", "E", "F"],num=math.floor (Arr.length*math.random ()); var text=arr[num]; Switch (text) {case "a": alert ("You have selected A package"); break; Case "B": alert ("You have selected B package"); break; Case "C": alert ("You have selected C package"); break; Case "D": alert ("You have selected D package"); break; Default: alert ("Welcome next visit");
In the switch statement, it is important to note that the statement after the case is executed only if the value of the condition is exactly equal to the value after the one, otherwise it will continue to be judged. After the break is encountered, the program will jump out of the statement structure of switch. When the value of the judging condition is not equal to all the values after the case, the program executes the statement after the default.
Operators and conditional statements in JS