

The following are some useful readings based of off topics covered in this article. Thanks for taking the time to read this article! I appreciate and value every recommend, comment ( nice or mean), and follow! I’ll be covering more topics in the near future about rewriting basic Javascript concepts using the new standards. Instead object literals as switches should become another tool in your javascript tool belt. The switch statement in JavaScript is used to select a particular group of statements to be executed among several other groups of statements. This does not mean that there are no uses for the switch statement or that object literals is always the right answer. The switch statement is a staple of many programing languages but has many inherent problems that can be solved by implementing an object literal solution instead. Just note the lack of a switch statement and the increased readability of our code.
Switch statement javascript devdocs code#
I wont go into to much detail on this code as I believe the breakdown of the simple example above explains how this redux store will work. Redux Store Reducers with Object Literals
Switch statement javascript devdocs how to#
The following example will show you how to implement your Redux store using an object literal switch. Lets say for example that you are currently using a switch statement for your Redux Store. In this use case I am not incredibility concerned with performance, more so readability. With a smaller number of cases a switch is the faster option, while when the number of cases grows a single lookup in a object literal becomes the faster option.

Performance for switch/if statements can be a concern when the number of cases becomes large. Additionally by using object literals all of the non standard “syntactical sugar” has been removed, simplifying code and increasing readability. This allows us to follow a cleaner more functional ES6 syntax. const dogSwitch = (breed) => brackets and consequently we removed the return statements. This statement will return a fact about our desired dog breed (Border Collie Fan Club). The Problemīelow we see a standard switch statement wrapped in a function. But the switch poses several problems, which I think are best shown through an example. The switch statement allows for cleaner organization of code when a large number of evaluations needs to be made. The humble switch statement, it exists in almost every programing language and javascript is no exception.
