###### tags: `JavaScript` # 【學習筆記】JavaScript 條件判斷:if else & switch case 在 JavaScript 中,常使用的條件判斷語法為:if else 與 switch case,使用時需注意兩者之間的差異:語法差異、可讀性、效能、使用時機等等。 ## [if else](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/if...else):將要判斷的內容放在條件式,當條件成立時執行下方的陳述式 ```javascript= let score = 66; if (score >= 90){ // 條件式 console.log(`很棒`); } else if (score >= 60 && score < 90){ console.log(`及格`); } else { console.log(`不及格`); } // 及格 ``` ## [switch case](https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Statements/switch):將「表達式的值」與「case 條件裡的值」做比對,執行符合此條件下方的陳述式 ```javascript= let score = 96; switch (true){ // 表達式 => 需注意這裡不是放 score 比對! case (score >= 90): // true === true console.log(`很棒`); break; case (score >= 60 && score < 90): // true !== false console.log(`及格`); default: // 條件均不符 console.log(`不及格`); } // 很棒 ``` ## 參考資料 + [[教學]IF ELSE與SWITCH CASE的比較](https://jameshsu0407.github.io/blog/20211023_if-else_switch-case/)
×
Sign in
Email
Password
Forgot password
or
Sign in via Google
Sign in via Facebook
Sign in via X(Twitter)
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
Continue with a different method
New to HackMD?
Sign up
By signing in, you agree to our
terms of service
.