Skip to content

Instantly share code, notes, and snippets.

@carlosrojaso
Created May 14, 2014 21:18
Show Gist options
  • Save carlosrojaso/94a617cc6586b2edf3c5 to your computer and use it in GitHub Desktop.
Save carlosrojaso/94a617cc6586b2edf3c5 to your computer and use it in GitHub Desktop.
JS if else
var stuffToDo = {
"bar": function() {
alert( "the value was bar -- yay!" );
},
"baz": function() {
alert( "boo baz :(" );
},
"default": function() {
alert( "everything else is just ok" );
}
};
// Check if the property exists in the object.
if ( stuffToDo[ foo ] ) {
// This code won't run.
stuffToDo[ foo ]();
} else {
// This code will run.
stuffToDo[ "default" ]();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment