Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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