Created
August 8, 2019 04:11
-
-
Save coding-chris-kao/7363c9474fda9cd9eab3e367486da693 to your computer and use it in GitHub Desktop.
Use switch like if-else in a flexible way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkAge(age){ | |
switch(true){ // Set boolean first | |
case (age > 18): | |
console.log('case 1') | |
break | |
case (age == 18): | |
console.log('case 2') | |
break | |
case (age < 18): | |
console.log('case 3') | |
break | |
default: | |
console.log("It didn't match any one") | |
break | |
} | |
} | |
// main | |
checkAge(12) // case 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment