The if-clause in JavaScript
The if-clause is equivalent to the Decision
element that we used in the last chapter. It checks whether a condition has been met.
The JavaScript code for an if
statement isn't that difficult. It is made up of a condition and the if-clause itself. The form looks like this:
Statement |
Example |
---|---|
if (condition) { code block } else if (condition) { code block } else { code block } | if (varString == "test") { varResult=true; } else { varResult=false; } |
In the preceding example, the varString
string is tested if it contains the word "test"
. If it does, the Boolean varResult
is set to true
. If it is not (the else
part), it is set to false
. The else if
block can be added multiple times.
Conditions and operators
A condition is made up of a statement that is either true or false. This statement is built by using operators. The following are the operators that JavaScript knows:
and |
or |
Not |
Equal |
Not equal |
Smaller |
Bigger |
---|---|---|---|---|---|---|
|
|
|
|
|
|