
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境設定
- TypeScript - 基本語法
- TypeScript 與 JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - Any
- TypeScript - Never
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - 符號
- TypeScript - null 與 undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策
- TypeScript - if 語句
- TypeScript - if else 語句
- TypeScript - 巢狀 if 語句
- TypeScript - switch 語句
- TypeScript - 迴圈
- TypeScript - for 迴圈
- TypeScript - while 迴圈
- TypeScript - do while 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - 函式構造器
- TypeScript - rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 擴充套件介面
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 訪問器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - Keyof 型別運算子
- TypeScript - Typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 雜項
- TypeScript - 三斜線指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixins
- TypeScript - 實用程式型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - if 語句
在 TypeScript 中,if 語句會評估一個條件(一個布林表示式),並且只有當條件為真時才會執行程式碼塊。程式碼塊執行之前會先評估條件。
如果條件為假,則會執行 else 後面的程式碼塊(如果存在)。我們將在下一章中更詳細地討論 if...else 語句。
語法
要編寫 if 語句語法,我們使用 if 關鍵字後跟括號中的條件,然後是包含在花括號({})中的程式碼塊。
if(boolean_expression) { // statement(s) will execute if the boolean expression is true }
如果布林表示式計算結果為真,則 if 語句內部的程式碼塊將被執行。如果布林表示式計算結果為假,則 if 語句結束後的第一組程式碼(在閉合花括號之後)將被執行。
流程圖
下面的流程圖顯示了 if 語句的工作原理。

示例
讓我們藉助 TypeScript 中的一些示例詳細瞭解 if 語句。
示例 1
在下面的示例中,我們定義了一個名為 num 的數字型別變數,並將其賦值為 5。由於條件計算結果為真,因此執行 if 語句的程式碼。
var num: number = 5 if (num > 0) { console.log("number is positive") }
編譯後,它將生成以下 JavaScript 程式碼。
var num = 5; if (num > 0) { console.log("number is positive"); }
上面的示例將列印“number is positive”,因為 if 程式碼塊指定的條件為真。
number is positive
示例 2
在下面的示例中,條件是一個布林變數 isQualified。如果 isQualified 為真,則 if 語句會執行其後的程式碼塊。
var isQualified: boolean = true; if( isQualified ) { console.log("Qualified for driving"); }
編譯後,它將生成以下 JavaScript 程式碼。
var isQualified = true; if( isQualified ) { console.log("Qualified for driving"); }
上面的示例將列印“Qualified for driving”,因為 if 程式碼塊指定的條件為真。
Qualified for driving
示例 3
在下面的示例中,我們定義了數字型別的變數 x 和 y,並分別為它們賦值 20 和 30。if 語句的條件是 x < y。對於這些給定的值,條件計算結果為真,因此執行 if 語句內的程式碼。
var x: number = 20; var y: number = 30; if (x < y){ console.log("x is less than y"); }
編譯後,它將生成以下 JavaScript 程式碼。
var x = 20; var y = 30; if (x < y){ console.log("x is less than y"); }
上面的示例將列印“x is less than y”,因為 if 語句指定的條件 (20 < 30) 為真。
x is less than y
示例 4:當條件為假時
var x: number = 100; var count: number = 0; if (x < 100){ count++; } console.log(count);
編譯後,它將生成以下 JavaScript 程式碼。
var x = 100; var count = 0; if (x < 100){ count++; } console.log(count);
由於條件 (x < 100) 計算結果為假,因此 if 程式碼塊將不會被執行。count 的值將保持與之前相同。輸出如下:
0