This is the final part of the JavaScript section. We are going to cover Boolean values, comparisons, conditional statements and events.
In the previous section, when we took a look at loops, we saw some comparison statements for the first time. In these comparison statements, we saw how if the statement returned TRUE, the loop would run and when it returned FALSE, it would not run. Well, these TRUE and FALSE values have a name and their own data type – they are referred to as Boolean values. Booleans are a data type that can only ever be TRUE or FALSE. In JavaScript everything is either TRUE or FALSE. Let’s look at exactly what that means now.
In JavaScript, everything has a value of either true or false, whether it be a string, an object, a variable, an expression. Let’s take a look at some examples of things that return true in JavaScript.
As you can see, these all have a value, and thus, are true. Conversely, anything without a value will return false. Some examples of things that return false are:
Because everything in JavaScript is either true or false, it allows us to use anything as a condition for a condition statement. This brings us nicely on to the topic of comparisons.
Comparisons in JavaScript are exactly as their name implies – they are ways of analysing and comparing different things in JavaScript to determine equality or difference in the statement. If something is ‘correct’ it is true; if incorrect, it’s false.
Let’s compare some statements against a variable we will now define with a value of 10: