Skip to content

Instantly share code, notes, and snippets.

@Ncreshon
Created June 11, 2017 17:09
Show Gist options
  • Select an option

  • Save Ncreshon/5fa5358521ed83f4697a926d0e976e94 to your computer and use it in GitHub Desktop.

Select an option

Save Ncreshon/5fa5358521ed83f4697a926d0e976e94 to your computer and use it in GitHub Desktop.
JS Bin[Control flow: if, else if, else, and switch statements]// source https://jsbin.com/giwafot
//Control flow or Conditional statements//
//Remember truthy and falsey 0, NaN, null, false, undefined, and empty strings//
//are false and will make your conditional statement false//
//if, else if, else Statements//
/* If and else statements work together to evaluate a logical expression
* and run different statements based on the result. if statements
* can be used alone but an else statement must be used with an if.
* The proper setup for an if statement is
* if(condition) {
* block of code executed if true
* } */
function example(num1, num2) {
if(num1 > num2) {
return num1 + ' is greater than';
}else if(num2 > num1) {
return num2 + ' is greater than';
}else {
return "Welp, I tried!"
}
}
console.log(example(3,3)); // will return "Welp, I tried!" since the//
//arguements are equal and neither is greater//
//Switch Statements//
/* Switch statements chooses between multiple statements (cases) to execute
* based on possible values of a single expression. A break statement
* should end the case. You can run 1 block of code for multiple cases.
* The poper setup for a switch statement is
*
* switch (epression) {
* case value:
* statement;
* break;
* case value:
* statement;
* break;
* }
*
*/
function exampleTwo(char){
switch (char) {
case 'Batman':
case "Superman":
case "Wonder Woman":
console.log("DC Hero");
break;
case "Joker":
case "Harley Quinn":
case "Riddler":
console.log("DC Villian");
break;
default:
console.log("Try again, Puddin!")
}
}
exampleTwo("Harley Quinn"); //DC Villian//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment