Better option from multiple statements in JavaScript

I'm using multiple if statements in JavaScript? Is there any better way to achieve it through JavaScript?

Last updated:7/19/2021 5:27:08 AM

1 Answers

Ethan Karla
Ethan Karla

It should depend on your use of the program. If you are making a program that has a lot of inappropriate conditions then you should go for the if condition. But if your condition depends on some kind of number or some string, you should replace if-else statements with switch case condition, where you have to decide if some 'case' happens, in which block of code is to be executed.

For better understanding switch to the example:

switch(status) {

    case 'active':
        //background green
        break;
    case 'onhold':
        //background yellow
        break;
    case 'inactive':
        //background red
        break;
    default:
        //background grey
        break;
}


Answer