Writing cleaner code by avoiding switch case and nested if in Javascript:
--
There are ways in which you can reduce number of lines of code and make the code looks cleaner.
One way is to avoid switch statements and nested loop
for example lets see the below functions:
simple switch statements:
here we are using switch ,
this code can be made cleaner as :
here we replaced , switch with an object literal .
Complex switch cases:
We have seen how to handle switch cases with single statement inside the switch cases. But what to do when there are multiple statements like :
What we could do is :
Here we replace switch with object literal and replaces the key values with functions.
Note that , you can write switch statement case in single lines also