In need of a help regarding a pretty conceptual and basic issue

Consider a pseudo code of this format:
if(condition-1 || condition-2 || condition-3) {
run these statements;
I need to determine which of my three conditions turned out true and hence the inner statements ran. I know switch-case is an alternative to do this but yet I wanted to accomplish through an if statement.

You would need either a switch or multiple ifs if you want to identify the specific condition(s) that is true.

function check(x){
    let which;

    if((which=-1, x==-1) || (which=-2, x==-2) || (which=-3, x==-3)){
        return which;

Would this work for you?

1 Like

@KamilCybulski - How does the comma between the two assignments work. I have not seen this before. You can just refer me to a related link for me to read. Thanks.

@rmdawson71 Check out MDN
Basically, to give you a picture

expression1, expression2

will evaluate both expression, but return only expression2. Obviously the solution I provided could’ve been written more gracefully, depending on how exactly if statement is used, but it should picture the idea.

OK, now I understand. It is little cryptic, but could come in handy for shortening code.


1 Like

Honestly, it would really depend on what type of conditions you are talking about. Do you have an example you can paste into a reply, so we can see what you are comparing. @KamilCybulski’s answer would work if you are checking if a variable is equal to something else, but not other situations.

Another way similar to @KamilCybulski’s answer for when checking whether a variable is equal to a certain value could be done with an Object.

function check(x){
    return {'-1':-1, '-2':-2, '-3':-3}[x];
check(-2)  // results in -2

@rmdawson71 I am just comparing the “innerHTML” of a few elements against a specific value. So yeah! its a simple comparison. I am just gonna try both the suggestions I got from you as well as from @KamilCybulski. Hopefully, it works! And, thank a lot for giving time in responding to this.