w3pop.com :: ÍøÂçѧԺ :: JavaScript :: JS If...Else
Conditional statements in JavaScript are used to perform different actions based on different conditions.
JSÖеÄÌõ¼þÓï¾äÒ»°ãÓÃÔÚÕë¶Ô²»Í¬µÄÌõ¼þÀ´Ö´Ðв»Í¬µÄ¶¯×÷¡£
If statement[IF Óï¾ä]
How to write an if statement.
ÊéдifÓï¾äµÄ·½·¨
If...else statement[IF...elseÓï¾ä]
How to write an if...else statement.
Êéдif..elseif..elseÓï¾äµÄ·½·¨
If..else if...else statement[if...elseif...elseÓï¾ä]
How to write an if..else if...else statement.
Êéдif..elseÓï¾äµÄ·½·¨
Random link[Ëæ»úÁ¬½Ó]
This example demonstrates a link, when you click on the link it will take you to W3Schools.com OR to RefsnesData.no. There is a 50% chance for each of them.
Õâ¸ö°¸Àý¾ÙÁËÒ»¸ö±ÈÀýÁ´½ÓµÄ°¸Àý¡£µ±Äãµã»÷ÏÂÃæµÄÁ´½Óʱ£¬Á´½Óµ½W3Schools.com»òRefsnesData.noµÄ¼¸Âʸ÷Ϊ50%
Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this.
ÔÚд´úÂëʱ¾³£»áÓöµ½Ïë¸ù¾Ý²»Í¬µÄÅжÏÀ´Ö´Ðв»Í¬µÄ¶¯×÷¡£Äã¿ÉÒÔÓüÙÉèÓï¾äÀ´×öµ½Õâµã¡£
In JavaScript we have the following conditional statements:
ÔÚJSÖÐÓÐÒÔÏÂһЩ¼ÙÉ裨Ìõ¼þ£©Óï¾ä£º
You should use the if statement if you want to execute some code only if a specified condition is true.
ÄãÓ¦¸ÃÔÚ´úÂëÔÚÖ»ÓÐÒ»¸ö×´Ì¬ÎªÕæµÄÇé¿öϾÍÖ´ÐеÄʱºòʹÓÃÕâÌõÓï¾ä
if (condition)
{
code to be executed if condition is true
}
|
Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!
×¢ÒâifÓï¾äÓ¦¸ÃÓÃСд£¬Ê¹ÓôóдµÄ»°»áÒýÆðJS´íÎó
<script type="text/javascript"> //Write a "Good morning" greeting if //the time is less than 10 var d=new Date()
var time=d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
</script>
|
<script type="text/javascript"> //Write "Lunch-time!" if the time is 11 var d=new Date()
var time=d.getHours()
if (time==11)
{
document.write("<b>Lunch-time!</b>")
}
</script>
|
Note: When comparing variables you must always use two equals signs next to each other (==)!
×¢Ò⣺Ҫ±È½Ï±äÁ¿Äã¾Í±ØÐëʹÓÃÁ½¸öµÈºÅ±ê¼Ç£¨==£©£¡
Notice that there is no ..else.. in this syntax. You just tell the code to execute some code only if the specified condition is true.
×¢ÒâÕâÀïûÓÐʹÓÃelse¡£ÄãÖ»ÊÇÈôúÂëµ±Ìõ¼þÎªÕæÊ±¾ÍÖ´ÐС£
If you want to execute some code if a condition is true and another code if the condition is not true, use the if....else statement.
Èç¹ûÄãÏëÌõ¼þÎªÕæÊ±ÔËÐÐһЩ´úÂë¶ø²»ÎªÕæÊ±ÔËÐÐÁíһЩ´úÂ룬¾ÍÓÃif...elseÓï¾ä
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
|
<script type="text/javascript"> //If the time is less than 10, //you will get a "Good morning" greeting. //Otherwise you will get a "Good day" greeting. var d = new Date()
var time = d.getHours()
if (time < 10)
{
document.write("Good morning!")
}
else
{
document.write("Good day!")
}
</script>
|
You should use the if....else if...else statement if you want to select one of many sets of lines to execute.
Èç¹ûÄãÏëÔÚ¼¸ÖÖÌõ¼þÖÐÑ¡ÔñÒ»ÖÖÈ¥Ö´ÐУ¬ÄǾÍÓ¦¸ÃÓÃif....else if...elseÓï¾ä
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and
condition2 are not true
}
|
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
else if (time>10 && time<16)
{
document.write("<b>Good day</b>")
}
else
{
document.write("<b>Hello World!</b>")
}
</script>
|