Skip to content

Instantly share code, notes, and snippets.

@kf6kjg
Last active October 22, 2017 16:32
Show Gist options
  • Save kf6kjg/14f1907cef9917ac265687c278f447ba to your computer and use it in GitHub Desktop.
Save kf6kjg/14f1907cef9917ac265687c278f447ba to your computer and use it in GitHub Desktop.
JavaScript inline switch. Useful in React render methods, and still feels like a switch statement with the checked value up top. Also allows for falsy values which I also didn't see in my casual search for similar solutions.
function iswitch(key, map, def) {
return map !== null && typeof map === 'object' && map.hasOwnProperty(key) ? map[key] : def; // This way falsy values can be returned.
}
let result = iswitch('a', {
'a': "first value",
'b': "second value",
}, "default value");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment