Writing cleaner code by avoiding switch case and nested if in Javascript:

Praveen David Mathew
1 min readApr 14, 2022

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 :

--

--

Praveen David Mathew
Praveen David Mathew

Written by Praveen David Mathew

An open source advocator/WebdriverIO Projectcommiter/Postman Supernova/Postman-html-extra contributor/Stack overflow sqa moderator/Speaker