JavaScript language basics 10
If statement in JavaScript:
Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=30;if(value1>value2){document.write("value1>value2");}</script>
In the above Code, the if statement is used to determine whether value1 is greater than value2. The result is certainly value1> value2:
Here we must place the condition in parentheses after the if keyword. When the condition is true, the code to be executed is enclosed in braces to form a good habit.
When determining conditions, some logical operators are involved, as shown in the following table:
Logical operators <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + icagicagicagicagicagicagicagicagqu5eo6jc37yt0 + large + 8rbvyo6kgicagicagicagicagicagicagicagicagicagia = "|
NOT (logical NOT )!
Logic and (&&):
Logic and result of the right operand of the left operand
True
False true false
True false
False
Conclusion: When all the conditional expressions are true, the result is true. Otherwise, the result is false.
Logic or (| ):
Logical or result of the right operand of the left operand
True
False true
True false true
False
Conclusion: when the expression is false, the result is false. Otherwise, the result is true.
Non-logical (!) :
Result of Non-logical operand
True false
False true
Conclusion: if the condition is false, the result is true. If the condition is true, the result is false.
Compound conditions can be used in JavaScript if statements, as shown in the following code:
Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=30;if(value1>value2){if(value1<50){document.write("value1>value2");}}</script>
In the code, value1> value2 is first judged. After the condition is true, value1 <50 is judged, and the condition is true. Output:
Of course this is correct, but the code is too bloated, so we use the above logical operators to achieve the above effect:
HTML>Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=30;if(value1>value2&&value1<50){document.write("value1>value2");}</script>
Result:
Reasonable Use of logical operators can reduce unnecessary code.
In this case, if we set value = 30, value = 40:
Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=30;var value2=40;if(value1>value2&&value1<50){document.write("value1 > value2");}if(value1
The result is as follows:
The result is correct. Now let's look at the code. If we want to judge that value1> value2 is not valid, We will output value1.
Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=30;var value2=40;if(value1>value2&&value1<50){document.write("value1 > value2");}else{document.write("value1 < value2");}</script>
In this way, the logic is clearer. if you need to determine whether value1 is equal to value2, you can use the if statement else if clause (of course, else if clause can be multiple to determine conditions in sequence):
Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=40;if(value1>value2&&value1<50){document.write("value1 > value2");}else if(value1
The result is as follows:
You can also write it as follows:
Hello World<script Language="JavaScript" TYPE="text/javascript">var value1=4;if(value1<=60&&value1>50){document.write("60--50");}else if(value1<=50&&value1>40){document.write("50--40");}else if(value1<=40&&value1>30){document.write("40--30");}else{document.write("value1 <= 30");}</script>
Determine the value1 interval. The result is as follows:
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/41253359 sentiment control _