5.1. Booleans¶
One of the core features of any programming language is the ability to conditionally execute a segment of code. This means that a program will run a segment of code only if a given condition is met.
Example
Consider a banking application that can remind you when a bill is due. The application will notify you that a bill is due soon, but only if the bill has not already been paid.
The condition for the above example is: Send a notification of an upcoming bill only if the statement "the bill is unpaid" is true. In order to state something like this in JavaScript, we need to understand how programming languages represent true and false.
5.1.1. Boolean Values¶
The JavaScript data type for storing true and false values is boolean
,
named after the British mathematician George Boole.
Fun Fact
George Boole created Boolean Algebra, which is the basis of all modern computer arithmetic.
There are only two boolean values---true
and false
. JavaScript is
case-sensitive, so True
and False
are not valid boolean values.
Example
1 2 3 | console.log(true);
console.log(typeof true);
console.log(typeof false);
|
Console Output
true
boolean
boolean
The values true
and false
are not strings. If you use quotes to
surround booleans ("true"
and "false"
), those values become strings.
Example
1 2 | console.log(typeof true);
console.log(typeof "true");
|
Console Output
boolean
string
5.1.2. Boolean Conversion¶
As with the number and string data types, the boolean type also has a
conversion function, Boolean
. It works similarly to the Number
and
String
functions, attempting to convert a non-boolean value to a boolean.
Try It!
Explore how Boolean
converts various non-boolean values.
1 2 3 4 5 6 | console.log(Boolean("true"));
console.log(Boolean("TRUE"));
console.log(Boolean(0));
console.log(Boolean(1));
console.log(Boolean(''));
console.log(Boolean('LaunchCode'));
|
5.1.3. Boolean Expressions¶
A boolean expression is an expression that evaluates to either true
or
false
. The equality operator, ==
, compares two values and returns true
or false depending on whether the values are equal.
Example
1 2 | console.log(5 == 5);
console.log(5 == 6);
|
Console Output
true
false
In the first statement, the two operands are equal, so the expression evaluates
to true
. In the second statement, 5 is not equal to 6, so we get false
.
We can also use ==
to see that true
and "true"
are not equal.
Example
console.log(true == "true");
Console Output
false
5.1.3.1. Comparison Operators¶
The ==
operator is one of six common comparison operators.
Operator |
Description |
Examples Returning |
Examples Returning |
---|---|---|---|
Equal ( |
Returns |
|
|
Not equal( |
Returns |
|
|
Greater than ( |
Returns |
|
|
Less than ( |
Returns |
|
|
Greater than or equal ( |
Returns |
|
|
Less than or equal ( |
Returns |
|
|
Although these operations are probably familiar, the JavaScript symbols are
different from the mathematical symbols. A common error is to use a single
equal sign (=
) instead of a double equal sign (==
). Remember that =
is an assignment operator and ==
is a comparison operator. Also note
that =<
and =>
are not recognized operators.
An equality test is symmetric, meaning that we can swap the places of the
operands and the result is the same. For a variable a
, if a == 7
is
true
then 7 == a
is also true
. However, an assignment statement is
not symmetric: a = 7
is legal while 7 = a
is not.
Warning
If you explore the equality operator in more depth, you will find some
surprises. For example, the following comparisons return true
:
7 == "7"
0 == false
0 == ''
We will explore the nuances of ==
in the upcoming section
Equality, and introduce two new operators, ===
and !==
, that
will align more closely with our intuitive notion of equality.
5.1.4. Check Your Understanding¶
Question
Under which conditions does Boolean
convert a string to true
?
Only when the string is
"true"
.Whenever the string contains any non-whitespace character.
Whenever the string is non-empty.
Never. It converts all strings to
false
.
Question
Which of the following is a Boolean expression? Select all that apply.
3 == 4
3 + 4
3 + 4 === 7
"false"