Conditions, Blocks and Semicolons


This is just a copy of my post about conditions blocks and semicolons to make it easier to for reference in the new forum:

The most simple conditional statement (1) looks like this:


Here statement is exactly 1 statement. In case you want an alternativ to the if you can extend it with an else (2):


Again statement and statement2 are only one statement (each). But what about using more than one statement in a case? (3)


here statement1 would belong to the condition and statement2 would be executed without condition. (4)


And here it would get even worse, as the else needs to follow
directly after the statement of the if or they will not be treated as
connected and you get an error as an else doesn't make sense without an

So how can we use more than one statement per case, what are these {} we usually use and why should we even bother about these cases mentioned above?

Well to have more than one statement per case we use the trick of working with blocks of code which start with { and end with } these blocks count as one statement regardless of the number of statements which are inside.(*)

The reason why we should have a look at this, although you could be
perfectly fine by using blocks all the time, even if they are not
necessary, is that we every now an then may encounter one of the errors
described above which are related to this ability.

e.g. a semicolon:

[Quote: From introduction post of AlbionsRefuge]
When you put a semicolon at the end of a condition, like this: if
(choice1 === choice2); you are telling the interpreter to consider that
as the end of your if statement. Any code following that will be
processed without regard for the outcome of that if condition.

That's pretty close, but the semicolon doesn't end the if statement
itself, it ends the statement of the if. This might be confusing, but
have a look at (1) and you could see that if(condition); just means:

  /*empty space*/;

so the semicolon creates an empty statement by ending the "nothing"
after the condition which would normaly be ignored and now the condition
is either finished or an else needs to follow directly after it, but
mostly it was just a mistake and it goes like this: if(condition);{...} and then you end up at the error described in (4).

Another use of this feature is that a conditional statement counts as one statement aswell, which lets us create an else if case by nesting:


could be formatted to:

else if(condition2)

(*) {} are used as blocks of codes for conditions and loops, functions and objects may use them in a different way.

PS: Link to "condition" after else problem:

PPS: And as an update where the use of semicolons is allowed:

Build "Rock, Paper, Scissors" - What if choice 1 is paper?
What am I doing wrong?
Another "Won't Push" topic
Can't seem to figure this out?!
11. Review, I am missing something
What is wrong?
5/7 Your second "for" loop
A question in one of the guides for semicolon formatting
If choice one is rock
Build Rock, Paper, Scissors Help!
Paper, rock, scissors confusion
Semicolon after curly brackets of functions?
6.What if choice 1 is rock?
Your second "for" loop (5/7)
SyntaxError: Unexpected keyword 'else' i can't understand where is wrong
I need help in solo flight code
I need help with number 6
12. Functions & if / else issue
I need help with number 6
Else as unexpected token
Why system always tell me "SyntaxError: Unexpected token '{'. Parse error."?
6/9 what's wrong? i checked many times but i dont know the error.please help
Hello, having problem with 8/9 "undefined"
Logical error in my code!
Java Script: is this system error?
6/9 keeps saying rock wins instead of paper
6/9 Unexpected token else
6/9 Unexpected token else?
4/9 unexpected token else
Rock Paper Scissors 4/9
Uh, IDK what is wrong with this! please help!
Whats wrong with my codes :(?
Adding Score - Help?
3/33 Expected ';'
12. Functions if/else problems
Need help with this program
8/13 'problem with syntax'
Syntax error
Someone explain this to me
7/9 help please
7 unexpected token "else"
12 Functions & if / else
6/9 issues please help! "SyntaxError: Unexpected token"
2. Code Your Own Adventure
My code won't work i need help it keeps saying SyntaxError: Unexpected token else
When should I use ;
12/13 Syntax error
2/9. Need help with syntax
4/9 SyntaxError: expected expression, got keyword 'else'
Help with 4/9
Syntax Check! =<
12. Function and if/else
6. List em' all! (nothing works)
Computer choice part 2: What am I missing here? Formatting quirk?
12. Functions if/else problems
Solo flight
Computer choice Part 2
12/13 SyntaxError: Unexpected token ILLEGAL
What if choice1 is paper - stuck
JS Introduction
Rock Paper Scissors Level 7
Computer choice, part 2... I'm completely stuck!
Help! I dont know whats wrong, unexpected number?
Help me please with this code
12. Functions & if / else issue
Stuck at 7. What if choice1 is paper? I've tried many things... and nothing
SyntaxError: Unexpected token else
Functions & if / else
Funciones e if / else
What if choice1 is rock? -- Unexpected keyword "else"
What if choice1 is paper
Question with 6. Unexpected token else
While loops again!
Head meet brick wall? Can anyone offer syntax help?
Why is that an infinite loop?
A little help?
9. Next steps
Unexpected token?
Build Rock, Paper, Scissors Lesson 8 JS
12. functions & if/else
Functions recap if/else. Cant see what is wrong
Illegal Return Statement, Computer Choice Part 2
#11 Review (while / do / for)
12. functions & if/else
Lesson 12
7. What if choice1 is paper?...Syntax Error Please Help
Error is coming again n again
Your code returned 'rock wins' instead of 'undefined' when the inputs are scissors and paper
What's wrong with this code?
9. the do/while loop
12. there's a problem with my syntax
Lesson 4 - omputer Choice - Part 2
Solo Flight - Very Frustrating!
For loop, while loop and do loop
Computer choice part 2 Syntax error token else? Help
6. What if choice1 is rock
What if choice1 is paper? Help
8. Functions in Javascript
Lesson 6 I don't know what I'm doing wrong
Computer choice part 2 Syntax error token else? Help
What if Choice 1 is Rock? Undefined 'else'
Got keyword else
"Unexpected token else"
Control Flow - "if / else if / else" using isNan
Number 7, What if choice1 is paper?
Computer Choice part 2, unexpected token {, what has gone wrong?
Lesson 4 Part 2. SyntaxError: Unexpected token else
What if choice 1 is paper? Syntax error
Lesson 12
HELP What If Choice 1 Is Paper - Build Rock, Paper, Scissors
Computer Choice: Part 2 Problem
Computer Choice 2: Wrong ranges?
12 function if/else
7/9 SyntaxError: expected expression, got keyword 'else'
SyntaxError: Unexpected token else
Unexpected token {
12 boucles et tableaux
Functions and If/Else where is the error in this syntax
Where have i gone wrong in my code?
AND exercise
5. your second "for loop"
Number 12 Functions and If/Else
I can't understand what is wrong
Looked in some posts still cant find problem
Console logs first letter of myName
What is wrong here
Your second "for" loop
5. Finding that Special Someone
Second "for" loop - again
Your second "for" loop, help me! my result - 1)))
Your second "for" loop
Why do i have to put an open curly brace after the if condition statement?
Want to do if/else statement with function
Could ya help me with my for loop?
27 Loop for Loop
3/12 SyntaxError: expected expression, got keyword 'else'
12. Fuctions & if/else
Please help me
12/13 Functions & if/else
What is wrong about this code?
Rock Paper Scissors unexpected token