Conditions provide a simple but powerful way to control the flow of execution through a piece of code. Loops allow you to perform repeating operations with less code. Let's take a look at:
if
conditions,switch
statements,while
,do-while
,for
, andfor-in
loops.
Let's start by clarifying what a block of code is, as blocks are used extensively when constructing conditions and loops.
A block of code consists of zero or more expressions enclosed in curly brackets.
{ var a = 1; var b = 3; }
You can nest blocks within each other indefinitely:
{ var a = 1; var b = 3; var c, d; { c = a + b; { d = a - b; } } }
Tip
Best Practice Tips
Use end-of-line semicolons. Although the semicolon is optional when you have one expression per line, it's good to develop the habit of using them. For best readability, the individual expressions inside a block should be placed one per line and separated by semi-colons.
Indent any code placed within curly...