Skip to content

Instantly share code, notes, and snippets.

@ivancorrales
Created December 5, 2015 06:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivancorrales/d108cc0dccc1fad460ae to your computer and use it in GitHub Desktop.
Save ivancorrales/d108cc0dccc1fad460ae to your computer and use it in GitHub Desktop.
Avoiding using if-else statements
function _ifElseFn(condition, cb, cb2){
var result = {true:cb,false:cb2};
return result[condition]();
}
function sayYourCarIsOld(){
console.log('your car is old');
}
function sayYourCarIsNew(){
console.log('your car is new');
}
var newCar = false;
_ifElseFn(newCar,sayYourCarIsNew,sayYourCarIsOld);
newCar = true;
_ifElseFn(newCar,sayYourCarIsNew,sayYourCarIsOld);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment