{"id":827,"date":"2014-10-31T18:26:51","date_gmt":"2014-10-31T18:26:51","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=827"},"modified":"2015-08-17T06:12:41","modified_gmt":"2015-08-17T06:12:41","slug":"converting-a-javascript-switch-statement-into-a-function-lookup","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/converting-a-javascript-switch-statement-into-a-function-lookup\/","title":{"rendered":"Converting a JavaScript switch statement into a function lookup"},"content":{"rendered":"<p>I just read a great post on <a href=\"http:\/\/us5.campaign-archive2.com\/?u=ea228d7061e8bbfa8639666ad&amp;id=be8ff9ee56&amp;e=9643791ef1\">WebTools Weekly<\/a> newsletter about how to convert a JavaScript switch statement into a function lookup. Here&#8217;s the full content from there, so be sure to check it out if you like it (it&#8217;s one newsletter a week, and no &#8211; I&#8217;m not affiliated with them in any way).<\/p>\n<p><span style=\"color: #606060;\">Because of the problems inherent in using JavaScript&#8217;s\u00a0<\/span><span style=\"color: #606060;\">switch<\/span><span style=\"color: #606060;\">\u00a0statement, many developers and reference guides will recommend avoiding\u00a0<\/span><span style=\"color: #606060;\">switch<\/span><span style=\"color: #606060;\">\u00a0constructs and instead using something called a<\/span><em style=\"color: #606060;\">method lookup<\/em><span style=\"color: #606060;\">\u00a0(also referred to as a\u00a0<\/span><em style=\"color: #606060;\">lookup table<\/em><span style=\"color: #606060;\">\u00a0or even\u00a0<\/span><em style=\"color: #606060;\">dispatch table).\u00a0<\/em><span style=\"color: #606060;\">Let&#8217;s convert a\u00a0<\/span><span style=\"color: #606060;\">switch<\/span><span style=\"color: #606060;\">statement to a method lookup, so we can see how this is done. Here&#8217;s our\u00a0<\/span><span style=\"color: #606060;\">switch<\/span><span style=\"color: #606060;\">:<\/span><\/p>\n<pre class=\"lang:default decode:true\">function doSomething(condition) {\r\n  switch (condition) {\r\n    case 'one':\r\n      return 'one';\r\n    break;\r\n\r\n    case 'two':\r\n      return 'two';\r\n    break;\r\n\r\n    case 'three':\r\n      return 'three';\r\n    break;\r\n\r\n    default:\r\n      return 'default';\r\n    break;\r\n  }\r\n}<\/pre>\n<p><span style=\"color: #606060;\">(<\/span><a style=\"color: #db7800;\" href=\"http:\/\/webtoolsweekly.us5.list-manage1.com\/track\/click?u=ea228d7061e8bbfa8639666ad&amp;id=7f0d8478ce&amp;e=9643791ef1\" target=\"_blank\">JS Bin example<\/a><span style=\"color: #606060;\">)<\/span><\/p>\n<p><span style=\"color: #606060;\">All of this is inside a\u00a0<\/span><span style=\"color: #606060;\">doSomething()<\/span><span style=\"color: #606060;\">\u00a0function, with a condition passed in. The possible values are &#8216;cased&#8217;, to define the return value. This is cleaner than a messy\u00a0<\/span><span style=\"color: #606060;\">if-else<\/span><span style=\"color: #606060;\">\u00a0construct, but can we clean it up even more?\u00a0<\/span><span style=\"color: #606060;\">Here&#8217;s basically the same thing in the form of a method lookup:<\/span><\/p>\n<pre class=\"lang:default decode:true  \">function doSomething (condition) {\r\n  var stuff = {\r\n    'one': function () {\r\n      return 'one';\r\n    },\r\n\r\n    'two': function () {\r\n      return 'two';\r\n    },\r\n\r\n    'three': function () {\r\n      return 'three';\r\n    }\r\n  };\r\n\r\n  if (typeof stuff[condition] !== 'function') {\r\n    return 'default';\r\n  }\r\n\r\n\r\n  return stuff[condition]();\r\n}<\/pre>\n<p><span style=\"color: #606060;\">(<\/span><a style=\"color: #db7800;\" href=\"http:\/\/webtoolsweekly.us5.list-manage.com\/track\/click?u=ea228d7061e8bbfa8639666ad&amp;id=462ff783c5&amp;e=9643791ef1\" target=\"_blank\">JS Bin example<\/a><span style=\"color: #606060;\">)<\/span><\/p>\n<p><span style=\"color: #606060;\">In the demo, you can see four logs in the console, one for each valid condition and then another one to demonstrate the default condition when the argument doesn&#8217;t match one of the methods.<\/span><br style=\"color: #606060;\" \/><br style=\"color: #606060;\" \/><span style=\"color: #606060;\">You can see why people like this technique. It&#8217;s clean, elegant, and seems a little more sophisticated without being more complex. I&#8217;ve only scratched the surface on this, so here are a few good resources to read up more on this subject:<\/span><\/p>\n<ul style=\"color: #606060;\">\n<li><a style=\"color: #db7800;\" href=\"http:\/\/webtoolsweekly.us5.list-manage1.com\/track\/click?u=ea228d7061e8bbfa8639666ad&amp;id=12885e82df&amp;e=9643791ef1\" target=\"_blank\">Don&#8217;t Use Switch<\/a>\u00a0in\u00a0<em>Programming JavaScript Applications<\/em>\u00a0by Eric Elliot.<\/li>\n<li><a style=\"color: #db7800;\" href=\"http:\/\/webtoolsweekly.us5.list-manage1.com\/track\/click?u=ea228d7061e8bbfa8639666ad&amp;id=b4b28912b9&amp;e=9643791ef1\" target=\"_blank\">Using Dispatch Tables to Avoid Conditionals in JavaScript<\/a>\u00a0by Josh Clanton<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>I just read a great post on WebTools Weekly newsletter about how to convert a JavaScript switch statement into a function lookup. Here&#8217;s the full content from there,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":828,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[],"class_list":["post-827","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/827","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=827"}],"version-history":[{"count":2,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/827\/revisions"}],"predecessor-version":[{"id":830,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/827\/revisions\/830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/828"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}