D
D
Dmitry2018-02-23 12:14:31
JavaScript
Dmitry, 2018-02-23 12:14:31

How to make multiple conditions in an if and have them all run?

There is a condition like this:

if(function1 && function2){
     // если обе возвращают true что то делаем. 
}


But here's the catch in if, both functions must be executed, and the && operator first checks one, if it's false then it doesn't even check the second.

Both functions return true or false from the result of the work. But they still have functionality that needs to be executed in both, regardless of the result.

I came up with options to write down with a comma)) it works, well, js hawala, I don’t know if it’s right.
I also came up with a variant with multiplication) true * true = 2 a true * true * false = 0
There is also an idea to return the number 0 or 1 and then calculate the sum and if it is equal to the number of functions then everything is ok.

Well, these are my options. Google does not give anything useful in this regard ..
It's just interesting to have which option is 100% correct. Call both functions in if

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
eRKa, 2018-02-23
@kaktys123

Everyone seems to have forgotten that there are two options for "AND", as well as for "OR".
The first option is "&" : both the left and right parts will be checked, regardless of what the left part returns.
The second option is "&&" : the right side will only be checked if the left side returns true.
Therefore, in your version, if you want the check to be performed in any case both on the left and on the right, then

if(function1 & function2){
     // если обе возвращают true что то делаем. 
}

R
res2001, 2018-02-23
@res2001

func1 = function1();
func2 = function2();
if(func1 && func2)
{
...
}

N
Nicholas, 2018-02-23
@healqq

The most logical thing is to take

functionality to be executed in both, regardless of the result
from these functions. You are doing so-so side effects, because of which you will suffer later.

D
Dmitry, 2018-02-23
@L1ar

function1 & function2

A
Adam Sulumov, 2018-02-23
@Markfictional

I did not understand the essence of your question) Try instead of "&&" - (and) use "||" - (or).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question