This week, in order to began examining conditional flow, we need to learn about an additional data type, which we will use extensively. This is the Boolean data type.
So far, we have discussed three specific data types;
4
3.14
-10000000
'This is a strings'
"Another String"
var myObj = { max: "happy", sally: "sad", peter: "indifferent" };
myObj.max
returns "happy"
We learned that all of these data types are stored within computers as a series of 1’s and 0’s. With each 1 or 0 representing a single bit. Like the storage mechanism of a single bit in the computer , the Boolean data type is used to represent the values of true and false (1
and 0
, respectively).
In other words, the Boolean data type is the simplest data type in a programming language or computer, as it can be represented by a single bit (i.e.
1
or0
). This means it is also one of the most fundamental concepts is computer science to understand, because all computer systems are based on testing everything as either1
or0
.
The Boolean is also sometimes known as the truth value, which is a “value indicating the relation of a proposition to truth”. This statement will become more evident as we go through the week. However, Booleans are typically used to represent whether something is true or false, a 1
or 0
, or the state of an element. These value states are often set in relation to some conditional test.
In JavaScript, the Boolean is represented by one of two keywords.
true
false
true
can also be thought of as the value ‘1’. Likewise, false
can be thought of as the value ‘0’.
The true
and false
keywords are “reserved keywords” within JavaScript, as such, the language will not allow you to overwrite them. (i.e. var true = "some value";
will “throw” and error and cause your program to crash.)
To set a variable to a Boolean value, simply pass the Boolean keyword to an assignment operator.
let myBool;
myBool = true;
myBool = false;