0
if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2)   return false ;
endgame();   return true;

this is the some JavaScript code which I'm not really understand, the code are using the simplest way to write the if-else statement but it makes me confuse about it, because how could it having three else with a same condition? the way that I interpret is

if(b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) {return false} else {endgame()} else {return true}

but I feel weird with this so I asked this question, I just want to know the correct syntax of this code. I think it may be my misunderstood of the code.

6
  • You cannot have two else clauses; just one. Commented Nov 24, 2018 at 16:49
  • @Pointy if you don't count else if as a else clause :) Commented Nov 24, 2018 at 16:52
  • 1
    @Jite well else if is not a clause at all; it's an else followed by the start of a new if statement. Languages differ in that regard, and JavaScript is like the C family of languages that don't have an elseif keyword. Commented Nov 24, 2018 at 16:55
  • Thanks for both explanation ! that was my big misunderstood.... Thanks for helping me! Commented Nov 24, 2018 at 16:57
  • All good, and I do know the difference, but for a person who asks a question as above, it might be hard to grasp the difference when told that you can only have one else when you can have as many else if as you wish. Semantics might be important, but sometimes it might confuse too. Commented Nov 24, 2018 at 16:59

1 Answer 1

3

It looks like a part of a function, because of the return statements. You get only one condition with a return statement and no else parts, because there are no one.

if (b1 < y2 || y1 > b2 || r1 < x2 || x1 > r2) {
    return false;
}
endgame();
return true;
Sign up to request clarification or add additional context in comments.

1 Comment

...wow that was a big misunderstood of the code ! Thanks for the correct explanation !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.