Skip to content

Instantly share code, notes, and snippets.

@coding-chris-kao
Created August 8, 2019 04:11
Show Gist options
  • Save coding-chris-kao/7363c9474fda9cd9eab3e367486da693 to your computer and use it in GitHub Desktop.
Save coding-chris-kao/7363c9474fda9cd9eab3e367486da693 to your computer and use it in GitHub Desktop.
Use switch like if-else in a flexible way
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