re: If/else or just if? VIEW POST

FULL DISCUSSION
 

definitely the just "if" boat here as much as possible, although I avoid single line and practice the bracket method.

 

@dechamp something like:

if (typeof val === 'string') {
    return 'A'
}
if (val === null || val === undefined) {
    return 'B'
}
return val

?

 

yup! that is the good stuff right there!

And based off your example I would do this...

check for no value first, save the code from type checking if it's not set.

if (!val) {
   return 'B';
}

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

return val;

You have a good point, but are you expecting your system to act like that? It's very rare you need to actually be checking for that, but if you do suspect it then definitely go for the finite checking.

But then again, I love using the "joi" package for my type checking.

Normally no, but in this case yes. It's a bare-bones project and the code is constructing a DB query from a set of known inputs, so null and undefined get treated as null, strings get quotes around them, and everything else is passed through.

code of conduct - report abuse