DEV Community

loading...

Discussion on: 5 ways to refactor if/else statements in JS functions

Collapse
kiddyxyz profile image
Muhamad Hudya Ramadhana

If you have more than one condition, why you just not using an array rather than or?

let arr = [null, undefined, '']
if (x in arr) {
   // code
}
Enter fullscreen mode Exit fullscreen mode
Collapse
sylwiavargas profile image
Sylwia Vargas Author

Yeah it's one way to do it but that still keeps the same if-else (or just no-return-statement) logic that we want to ideally get rid of. Here I'd also worry about the performance as if-else is costly and so is looping and here we'd have looping inside if-else. But I like this example — thank you!