Friday, April 01, 2022

Empty Array, truthy or falsey?

I had to switch from this:
return (matching_r) ? matching_r : -1;

to this:
return (matching_r.length) ? matching_r : -1;

to cover the cases when there's an empty array because I was never hitting minus one with the first statement.

Empty strings are falsey. Zero, of course, is falsey so I thought an empty array would be falsey. But as it turns out, that's not the case. Note about the syntax: Paranthesis around the condition is not required.

Post a Comment

Note: Only a member of this blog may post a comment.