DEV Community

Discussion on: If/else or just if?

Collapse
 
gypsydave5 profile image
David Wickes • Edited

Another option (for those who favour expressions over statements) can be built out of JavaScript's ternary operator:

return (typeof val === 'string') ? 'A'
     : (val === null || val === undefined) ? 'B'
     : val

Quite a popular pattern.

Collapse
 
tarialfaro profile image
Tari R. Alfaro

Ternary operators are really nice for simple and small statements, but I definitely wouldn't recommend them for anything complex. Makes things to hard to read for me. The same could be said for other developers.

Collapse
 
byrro profile image
Renato Byrro

That's not easy to read at all in my opinion...