The IF statement in javascript:
<span style= "FONT-SIZE:18PX;" >
In the above code, the IF statement to determine whether value1 is greater than value2, the result is definitely value1>value2:
Here we must place the judging condition in parentheses after the IF keyword, and when the condition is true, the code to be executed is enclosed in curly braces to form a good habit.
When judging a condition, it is sometimes involved in the operation of some logical operators, such as the following table:
Logical Operation Operators
and (Logic and) &&
or (logical OR) | |
Not (logical)!
Logic and (&&):
Left operand right operand logic and result
True True True
False true False
True false False
False false
Conclusion: When the conditional expression is all true, the result is true, whereas the result is false.
Logic or (| | ):
Left operand logical or result of right operand
True True True
False True True
True False True
False false
Conclusion: When the conditional expression is all false, the result is false, whereas the result is true.
Logical NON (! ):
Result of operand logic not
True False
False true
Conclusion: When the condition is false, the result is true; when the condition is true, the result is false.
A compound condition can be used in a JavaScript if statement, as in the following code:
<span style= "FONT-SIZE:18PX;" >
The Code first Judge Value1>value2, the condition is established, Judge Value1<50, the condition establishes the output:
Of course this is correct, but the code is too bloated, so we use the above logical operators to achieve the above effect:
<span style= "FONT-SIZE:18PX;" >html>
Results:
Reasonable use of logical operators can reduce some unnecessary code for us.
At this point if we will value=30,value=40:
<span style= "FONT-SIZE:18PX;" >
The results are as follows:
The result is correct, we look at the code now, if we want to judge Value1>value2 is not established, the output value1<value2, obviously the above code is too long, we can use the IF statement of the ELSE clause to achieve:
<span style= "FONT-SIZE:18PX;" >
In this case, the logic is clearer, and if you need to determine if value1 is equal to value2, we can use the IF statement else if clause (of course else if clause can be more than one, to judge the condition in sequence):<span style= "FONT-SIZE:18PX;" >
The results are as follows
Of course, it can be written as follows:
<span style= "FONT-SIZE:18PX;" >
Judging the interval of value1, the result is as follows:
Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/41253359 Emotional Control _
JavaScript Language Basics 10