Shoud return false

So I wrote:

function truthCheck(collection, pre) {
  //console.log(!!collection[0][pre])
  for (let i=0; i<collection.lengtth; i++){
    if(!collection[i].hasOwnProperty(pre)) {
      return false; 
    }
    if(!!!collection[i][pre]){
      return false; 
    }
  }
  return true; 
}

truthCheck([{"single": ""}, {"single": "double"}], "single");

console log prints out false so second if statement should be true.

Why 3 !s?

I’m checking if the property values truthy value is false

You only need one or two. One flips truthy to false and falsy to true. Two converts a value to its boolean equivalent.

Three is the same as one.

I just saw this typo.

Ok was it wrong to put three returns?

I don’t understand the question. I haven’t said anything about three return statements.

I pointed out that three exclamation points is excessive and highlighted a typo you made.

Yes I know, I’m saying my solution technically works now after changing typo and changing it to one ! but is it bad to have three returns new question.

No problem at all to have multiple returns.

I much prefer the return early style over a giant if/else chain. Of course everyone has their own opinions, but I would say that in the current zeitgeist else is generally avoided.

By the way, the if statement checks truthiness, so even !! is redundant.

Also, trying to access a property that does not exist on an object returns undefined, which is falsy, so the following lines:

if(!collection[i].hasOwnProperty(pre)) {
  return false; 
}
if(!!!collection[i][pre]){
  return false; 
}

could be shortened to simply:

if (!collection[i][pre]) {
  return false;
}
1 Like

I don’t know that else is ‘bad’. Use it where it fits.

I think you can write this much more compactly with the .every method, and I generally prefer a more functional style, so that’s how I’d do it.

With your approach, technically your two false return paths are redundant, as undefined is falsy.

1 Like

I wouldn’t call it bad either! I guess it’s very common to see beginners writing code like this

let result;
if (condition) {
  result = "something";
} else {
  result = "something else";
}
return result;

where the following is arguably more understandable and does the same thing

if (condition) {
  return "something";
}
return "something else";

Of course, there are plenty of situations where else is absolutely necessary. As you said, use it where it fits.

Yeah, sometimes people get excessively verbose in weird ways.

I come from a C background, so I have mixed feelings about early return. It can tidy up code, but I tend to use it sparingly. I try to limit the number of ways to exit a function.

I think the sweet spot for early returns is found in the land of small, pure functions, which I imagine is far less popular in C than in JS these days. Completely talking from my butt though, I don’t know anything but web programming.

I think it depends upon application more than language? Scientific code often can take 100+ lines to do ‘one thing’ in a function. That’s my main area of experience.

But tying back to OP’s question, I see multiple return statements as a fine style choice in this code. I’d just ditch the duplicate if statement.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.