Conditional statements in JavaScript are used to perform different actions based on different conditions.
JS中的條件陳述式一般用在針對不同的條件來執行不同的動作。
Examples
例子
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%
Conditional Statements
假設語句
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中有以下一些假設(條件)語句:
- if statement - use this statement if you want to execute some code only if a specified condition is true
這條語句一般是在代碼在只有一個狀態為真的情況下就執行的時候使用
- if...else statement - use this statement if you want to execute some code if the condition is true and another code if the condition is false
兩個狀態,一種為真,還有種不為真,分別執行不同動作。
- if...else if....else statement - use this statement if you want to select one of many blocks of code to be executed
你想在多個條件中選擇一個或幾個去執行,就用這個
- switch statement - use this statement if you want to select one of many blocks of code to be executed
在許多條件中選擇一個去執行,用這個
If Statement
if語句
You should use the if statement if you want to execute some code only if a specified condition is true.
你應該在代碼在只有一個狀態為真的情況下就執行的時候使用這條語句
Syntax
文法
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錯誤
Example 1
例子1
<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> |
Example 2
例子2
<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...else Statement
If...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語句
Syntax
文法
if (condition){code to be executed if condition is true}else{code to be executed if condition is not true} |
Example
例子
<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> |
If...else if...else Statement
If...else if...else 文法
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語句
Syntax
文法
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 andcondition2 are not true} |
Example
例子
<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> |