The operators in JS and the branching structure in JS

Source: Internet
Author: User
Tags bitwise operators logical operators

The operators in JS

1. Arithmetic operation (single-mesh operator)
+ 、-、 *,/,% withdraw, + + self-increment 、--self-reduction
+: Two functions, link string/addition operation. The addition is performed when the + sides are all numbers, and the link string acts when the + side has either side as a string.
The result after the link is a 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;
--: Self-increment operator, the variable is on the original basis-1;
"The 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;
② different points: a++ first with the value of a to calculate, then the a+1;
++a first the value of a of +1, after using the value of a+1 to calculate;
Eg:var A=3,b,c;
b = a++ +2;//first with a operation, b=3+2, and then a+1=4;
c = ++a +2;//first put a+1,a=5, and then use the value of a+1 later operation.

2. Assignment operation
= Assignment = = = *=/=%=
+ =: a +=b; equivalent to A=a+b; but the former is faster than the latter, so it is recommended to use + =;

3. Relational operations
= = equals, = = = Congruent,! = Unequal,! = = Not congruent, >, <, >=, <=
① relational operator, the result after operation, can only be a Boolean type;
② determine whether a number is in a range than required && link;
Alert (a<10 && a>0); √10>a>0x
= = = Strictly, requires not only the same type, the value must be the same, different types, the result is directly false, the same type, in the next judgment;
= =: equals. The same type as the = = = effect. When the type is different, it will try to convert both sides to numbers number before judging;
But there are individual exceptions, such as: null==flasexnull==undefined√

4. Conditional operator (multi-mesh operation)
A>b?true:false
Have two important symbols? And:
When? Executed when the previous operation result is true: the preceding code
When? When the previous operation evaluates to False, execute: The following code
A colon can be a numeric value, and the entire formula can be used to assign Var a=1<2?1:2;
The code block can be executed directly on either side of the colon; 1<2?alert{1}:alert (2)
Multi-mesh operators can be nested in multiple layers. var a =1<2?alert (1):(1>0?4:5);

5, bitwise operators, logical operators
&& and, | | Or! Non -
&& true on both sides of the set result
|| On either side of the set, the result is true

6. Precedence of Operators
() parentheses Highest
! + +--monocular operator
* / %
+ -
> < > = <=
== !=
&&//With or simultaneous presence of,&& | | High
||
= + = = *=/=//Minimum is a variety of assignments

The branching structure in JS

[if-else structure "
1. Structure:
if (judging condition) {
condition is true, execute if {}
}else{
condition is false, execute else {}
}


2, caveats:
①else{} statement block, which can be omitted as appropriate. {} after
②if and else can be omitted, but {} is omitted, if and else can only follow one statement, so it is not recommended to omit {}.


3, if's () condition, supported by:
①boolean True, False is False,
②string: empty string is false, all non-empty strings are true,
③number:0 is false, all non 0 digits are true;
④null/undefined/nan: All false;
⑤object : All is true;

"multiple if. Step if structure"
1. Structure:
if (condition one) {
Condition one is established, action performed
}else if (condition two) {
Condition one is not established && Condition II is established, the operation is performed. The action that is performed when all the conditions above
}else{
are not true.
}

2, multiple if structures, each judgment condition is mutually exclusive, execution chooses one of the paths to execute. After encountering the correct options and executing, jump out of the structure and no longer judge the subsequent branches;

"Nested IF Structure"
1, the structure of the wording:
if (condition i) {
Condition One established
If () {
Condition I set up && condition II established
}else{
Condition one is established and condition two is not established.
}


}else{
Condition one is not tenable
}
2, in nested if, if omitted {} then else will always belong to his nearest if structure;

3, nested structure can be nested multiple layers, but generally not recommended more than 3 layers;
It is generally not recommended to use nested if structures with multiple if structure.

How to do a simple calculator:

<script>
var a =parsefloat (Prompt ("Please enter the first integer:"));
var b =prompt ("Please enter symbol:");
var c =parsefloat (Prompt ("Please enter the second integer:"));

var result = 0;
result = b== "+"? A+c:result;
result = b== "-"? A-c:result;
result = b== "*"? A*c:result;
result = b== "/"? A/c:result;
Alert ("The result is:" +result);
</script>

Ii

<script>
var num1 =parsefloat (Prompt ("Please enter first integer:"));
var fu =prompt ("Please enter the operation character");
var num2 =parsefloat (Prompt ("Please enter the second integer:"));

if (fu== "+") {
alert (NUM1+NUM2);
}else{
if (fu== "-") {
alert (NUM1-NUM2);
}else{
if (fu== "*") {
alert (NUM1*NUM2);
}else{
alert (NUM1/NUM2);
}
}
}
</script>

The operators in JS and the branching structure in JS

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.