Basic JavaScript language 10 and javascript language 10

Source: Internet
Author: User

Basic JavaScript language 10 and javascript language 10

If statement in JavaScript:

<span style="font-size:18px;"><HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=30;if(value1>value2){document.write("value1>value2");}</SCRIPT></BODY></HTML></span>

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

AND (logical AND )&&

OR (logical OR) |

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:

<span style="font-size:18px;"><HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=30;if(value1>value2){if(value1<50){document.write("value1>value2");}}</SCRIPT></BODY></HTML></span>

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:



<span style="font-size:18px;">HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=30;if(value1>value2&&value1<50){document.write("value1>value2");}</SCRIPT></BODY></HTML></span>

Result:


Reasonable Use of logical operators can reduce unnecessary code.


In this case, if we set value = 30, value = 40:

<span style="font-size:18px;"><HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=30;var value2=40;if(value1>value2&&value1<50){document.write("value1 > value2");}if(value1<value2){document.write("value1 < value2");}</SCRIPT></BODY></HTML></span>

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 <value2. Obviously, the code above is too long, we can use the else clause of the if statement to implement the following:

<span style="font-size:18px;"><HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><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></BODY></HTML></span>

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):

<span style="font-size:18px;"><HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><SCRIPT Language="JavaScript" TYPE="text/javascript">var value1=40;var value2=40;if(value1>value2&&value1<50){document.write("value1 > value2");}else if(value1<value2){document.write("value1 < value2");}else{document.write("value1 = value2");}</SCRIPT></BODY></HTML></span>

The result is as follows:



You can also write it as follows:

<span style="font-size:18px;"><HTML><HEAD><TITLE>Hello World</TITLE></HEAD><BODY BGCOLOR="WHITE"><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></BODY></HTML></span>

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 _










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.