JavaScript Control Structures & Arrays
Control Structures
Conditional Statements
Conditional statements can take the following forms:
if (condition)
statement
else if (condition)
statement
else
statement
condition ? if_true_expr : if_false_expr
Switch Statements
We can use switch
statements like so:
switch (expr) {
case expr1:
statements
break;
case expr2:
statements
break;
default:
statements
break;
}
while
& do
while
Loops
We can use these two different types of while
loop like so:
while (condition)
statement
do
statement
while (condition)
For Loops
We can write for
loops with the following syntax:
for (i = 1, j = 1; j >= 0; i++, j--)
document.writeln(i = " * " + j + " = " + i*j)
Variables declared in the for
loop are still visible outside the loop. We can use let
, instead of var
, to declare a variable that is local to a block.
break
& continue
We can use these statements in a loop to modify the program flow:
break
- Stops execution of the loop and continues with the rest of the program.continue
- Stops execution of the current iteration of the loop and skips to the next iteration.
Error Handling
When a JavaScript statement generates an error, an exception is thrown. We can use the following structure to handle this behaviour:
x = "A"
try {
if (isNaN(x)) throw "x is NaN"
y = x.toFixed(2)
} catch (e) {
document.writeln("Caught: " + e)
y = 0
} finally {
document.writeln("y = ", y)
}
You can also see that we can use throw
to generate exceptions.
Arrays
An array is created by assigning an array value to a variable:
var arrayVar = [elem0, elem1, ...]
We can find the length of an array like so:
var length = arrayVar.length
Assigning a value to arrayVar.length
will extend, or shrink, the array to this new length.
We can return a copy of any array by using the slice
function:
var newArray = arrayVar.slice(start, end)
This can be useful as, by default, arrays are passed by reference.
Other Array Operators
JavaScript has no stack
or queue
data structures. We do have the following functions though:
Function | Description |
---|---|
array.push(value1, value2, ...) |
Appends one or more elements to the end of an array. Returns the number of elements in the resulting array. |
array.pop() |
Extracts the last element from an array and returns it. |
array.shift() |
Extracts the first element of an array and returns it. |
array.unshift(value1, value2, ...) |
Inserts one or more elements at the start of an array variable. Returns the number of elements in the resulting array. |
Iterating on Arrays
The recommended way to iterate over all elements of an array is a for
loop:
for (i = 0; i < arrayVar.length; i++) {
... arrayVar[i] ...
}
We can also use the forEach
method like so:
var callback = function (elem, index, arrayArg) {
statements
}
arrayVer.forEach(callback);
- The
forEach
method takes a function as an argument. - It iterates over all elements of the array.
- It passes the current array element
elem
, to the current indexindex
and a pointer to the arrayarrayArg
to the function. - Return value of that function is ignored.