JS operator and If,switch condition judgment

Source: Internet
Author: User

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=66,b,c,d,e,f; b= a+10;  c=a-10;     d=a*10;  660  E=A/10;    6.6 f=a%4;    2

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)         //data type, value equal

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.

The If statement plays a very important role as the conditional judgment statement in JS.

Its syntax is: if (condition) Statements1 else statement2

 1  " Span style= "COLOR: #000000" >var goal=40+parseint (60*math.random ());  2   if (goal>=80) { 3   document.write (' excellent score: ' +goal+ ' points!) ")  4  }else if (goal>=60) { 5   document.write ("score qualified:" +goal);  6  }else{ 7< /span>  document.write ("score Unqualified: +goal+")  8 }  
Math.random () randomly takes a number from 0 to 1.
Parseint () is converted to integers, ignoring the decimal point.

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
    }

1 var arr=["A", "B", "C", "D", "E", "F"],num=math.floor (Arr.length*math.random ());2 var text=arr[num];3 switch (text) {4 Case "A":5 alert ("You have selected a package");6 Break ;7 Case "B":8 alert ("You have selected the B package");9 Break ;Ten Case "C": One alert ("You have selected C package"); A Break ; - Case "D": - alert ("You have selected D package"); the Break ; - Default: - alert ("Welcome next visit"); -}
Arr.length*math.random () indicates that the length of the array is multiplied by the number from 0 to 1.
The Math.floor () method performs a down-rounding calculation that returns an integer that is less than or equal to the function parameter and closest to it.
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.
 

JS operator and If,switch condition judgment

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.