Javascript conditional return

let obj = {

  true1: true,

  true2: true,

  true3: true,

};

let true1 = true;

let true2 = true;

let true3 = true;

if (true1) {

  return obj.true1;

}

if (true1 && true2) {

  return obj.true1 && obj.true2;

}

if (true2 && true3) {

  return obj.true2 && obj.true3;

}

if (true1 && true3) {

  return obj.true1 && obj.true3;

}

if (true1 && true2 && true3) {

  return obj.true1 && obj.true2 && obj.true3;

}

I want this to happen in a short way.
Instead of checking one by one. Just wanna store those return variables in a data lets call as X.
And finally wanna return that final variable X just like => return X
I wanted to demonstrate a bit what I am trying to explain in below code.
(Which is not a right way to do that.) I wanna learn what is actually the right way ?

let returnvalue;

if (true1) {

  returnvalue += obj.true1;

}

if (true2) {

  returnvalue += obj.true2;

}

if (true3) {

  returnvalue += obj.true2;

}

return returnvalue; (wanted to achieve to returnvalue such as : 
returnvalue ==> obj.true1 && obj.true2 && obj.true3)

And just do not get bothered with the variable names. I created them so that I can explain my problem in more clear way.

I know if it was the string case I would do that just like ;

let returnValue ="";

if(true){
returnValue+="aValue"
}
if(true}{
returnValue+="bValue"
}

return returnValue;

But since return variables are not string, how to do combine them in proper way ?

I am confused at one thing and wanted to ask to you guys. My concern is in code comments. Thanks in advance.

Are you asking about &=

Hey there. Thanks for answering. But no. I wanted to learn how to combine return values which are not String type. I mean I know if return values were to be string, I could do that like below.

let returnValue ="";

if(true){
returnValue+="aValue"
}
if(true}{
returnValue+="bValue"
}

return returnValue;

Not sure what you mean by “combine return values which are not String type”. How are you wanting to “combine” them exactly? Do you have an example?

2 Likes

Combine how though? You can add a variable that you will return just like you can for any other variable. You can and it, or it, or anything else you can do with a variable.

let returnvalue;

if (true1) {

  returnvalue += obj.true1;

}

if (true2) {

  returnvalue += obj.true2;

}

if (true3) {

  returnvalue += obj.true2;

}

return returnvalue; (wanted to achieve to returnvalue such as : 
returnvalue ==> obj.true1 && obj.true2 && obj.true3)

By combining I meant that;

return obj.true1 && obj.true2 && obj.true3

So… You can use the and operator just fine

valueToReturn = valueToReturn && otherValue

Your example is returning the evaluation of an expression containing several boolean ANDed values. So I assume what you mean by “combine” is evaluate the expression consisting of series of ANDed boolean values?

Edit: If what I describe above is what you want and the object passed to the function would only have properties with boolean values, then the following could work:

const obj1 = {
  true1: true,
  true2: true,
  true3: true
};


const obj2 = {
  true1: true,
  true2: false,
  true3: true
};

function evalObjBooleans(obj) {
  return Object
    .keys(obj)
    .reduce((exp, key) => exp && obj[key]);
}

evalObjBooleans(obj1); // true
evalObjBooleans(obj2); // false

OR

function evalObjBooleans(obj) {
  return Object
    .keys(obj)
    .every(key => obj[key]);
}

Okay I guess my example did not explain enough what I was trying to explain. Sorry for that. I will share the actual code that I ve been working on.

I am trying to do in memory search based on input value. Again you better do not bother with the variable names. Since they are a bit confusing and related to database keys.

    {studentData
                      .filter(student => {
                        if (
                          searchTerm.name !== '' &&
                          searchTerm.department !== '' &&
                          searchTerm.phone !== '' &&
                          searchTerm.studentno !== ''
                        ) {
                          return (
                            student.userId.name.includes(searchTerm.name) &&
                            student.departmentId.name.includes(searchTerm.department) &&
                            student.userId.phone.includes(searchTerm.phone) &&
                            student.studentno.includes(searchTerm.studentno)
                          );
                        } else if (
                          searchTerm.name !== '' &&
                          searchTerm.department !== '' &&
                          searchTerm.phone !== ''
                        ) {
                          return (
                            student.userId.name.includes(searchTerm.name) &&
                            student.departmentId.name.includes(searchTerm.department) &&
                            student.userId.phone.includes(searchTerm.phone)
                          );
                        } else if (searchTerm.name !== '' && searchTerm.department !== '') {
                          return (
                            student.userId.name.includes(searchTerm.name) &&
                            student.departmentId.name.includes(searchTerm.department)
                          );
                        } else if (searchTerm.name !== '') {
                          return student.userId.name.includes(searchTerm.name);
                        }
                      })

So above code, I am checking the search term object. By default searchTerm key values is empty string. Whenever user type something to related input, I catch that value and setting event.target.event to search term value. So instead of checking one by one for every object keys, I want this to happen just like below,


let returnValue ;
if( searchTerm.name !== ''){
returnValue += student.userId.name.includes(searchTerm.name)

}
if( searchTerm.department!== ''){
returnValue += student.userId.name.includes(searchTerm.name)
}

if( searchTerm.phone!== ''){
returnValue += student.userId.name.includes(searchTerm.name)
}

if( searchTerm.studentno!== ''){
returnValue += student.userId.name.includes(searchTerm.name)
}


return returnValue

I want it to happen like this.

So you are literally wanting to return a string of boolean values? The above would return something like "truefalsefalsetrue" with no spaces between the boolean text values.

The above code is wrong. I am aware of that. I am just asking that, since there is a way to do that with string, I mean we can combine the return values. But what if return values are not string type, how do we combine them. Is there any way to do that ? Or we just have to check one by one if else if else if … else and so on

What is the desired output? A string? A true/false? What?

It seems like the desired return value is a Boolean (true/false).

That would be my guess too. So I’m confused as to why using = and && instead of += isn’t an acceptable solution?

Yes it is boolean. So;

let returnValue ;
if( searchTerm.name !== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)

}
if( searchTerm.department!== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)
}

if( searchTerm.phone!== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)
}

if( searchTerm.studentno!== ''){
returnValue &&= student.userId.name.includes(searchTerm.name)
}


return returnValue

Okay I think this should work ? No ?

What value should returnValue have if searchTerm.name, searchTerm.department, searchTerm.phone, and searchTerm.studentno are all empty? With your current code above, you would be returning the value undefined.

Let me ask you a different question which will help me to understand exactly what you are wanting here. Let’s say all four searchTerm property values are non-empty. Also, let’s say that the first three property values are not included in their respective student property value, but the last one studentno was included. What should the return value be?

I was actually really having brain overload when I started to ask the question.

Now I get the idea.

Thank you guys for sharing time for the question. Have a nice day !

@RandellDawson @JeremyLT

This was the what I was trying to achieve. And its final and working code …!

Now I am able to perform in memory search…

 {studentData
                      .filter(student => {
                        let returnValue = true;
                        if (searchTerm.name !== '') {
                          returnValue =
                            returnValue && student.userId.name.includes(searchTerm.name);
                        }
                        if (searchTerm.department !== '') {
                          returnValue =
                            returnValue &&
                            student.departmentId.name.includes(searchTerm.department);
                        }
                        if (searchTerm.phone !== '') {
                          returnValue =
                            returnValue && student.userId.phone.includes(searchTerm.phone);
                        }
                        if (searchTerm.studentno !== '') {
                          returnValue =
                            returnValue && student.studentno.includes(searchTerm.studentno);
                        }

                        return returnValue;
                      })

Glad you got it working as you want. Based on your current logic it only takes one searchTerm that is non-empty and is not included in the student property, to end up excluding the student from the final array. Is that the desired result?

Thank you. The below code is searchTerm state.

  const [searchTerm, setSearchTerm] = useState({
    studentno: '',
    name: '',
    department: '',
    phone: ''
  });

The current logic is, if any value in search term is non empty, I compare it with the database value which is student property. If student property includes it then I return true, and eventually I return the related student with the search term. Search term may take multiple non empty values. Thats why I am combining them in returnValue.