Created
May 14, 2014 21:18
-
-
Save carlosrojaso/94a617cc6586b2edf3c5 to your computer and use it in GitHub Desktop.
JS if else
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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