DEV Community

Discussion on: If/else or just if?

Collapse
 
timkilian profile image
Tim Kilian

It depends on the case, but I would probably pick a mixed case like this:

if (typeof val === 'string') {
return 'A';
}
if (!val) {
return 'B';
}
return val;

The main reason is because blocks are in my opinion better to see and less confusing. I also try to avoid the tenary operator, but it really depends on the case. Sometimes it's just perfect. There is no need to write an else in this case. The else if wouldn't improve any performance, and for me it's better to have no else case at all.