Booleans - Griffith-ICT/1701ICT-Creative-Coding GitHub Wiki
A boolean is a data type which has two possible values: true or false.
Most programming languages have a concept of booleans.
Apart from the direct values of true and false, comparison operators also result in a boolean value.
For example for our if statements we have been comparing numbers.
5 < 10 results in the boolean value true
12 < 10 results in the boolean value false
The if statement expects a boolean value:
if (boolean) {
}
For example the following if statement will always execute:
if (true) {
}
The for loop also requires a boolean for its condition. Note that we use a comparison operator:
for (var i = 0; i < 10; i++) {
}
The following for loop will loop forever:
for (var i = 0; true; i++) {
}
A for loop will only end when its condition becomes false, this for loop will never execute its body because it always checks the condition before executing the body:
for (var i = 0; false; i++) {
}
The order of execution for for loops is:
- Initialisation (e.g.
var i = 0) - Condition (e.g.
i < 10) - Execute the body
- Increment (e.g.
i++)
Relational operators
Here is a list of most of the relational operators supported by JavaScript
<- Less than<=- Less than or equal to>- Greater than>=- Greater than or equal to==- Equal to!=- Not equal to
You can use these in if statements and for loops.
Next: Event Handling