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.
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
1console.log(true);
2console.log(typeof true);
3console.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
1console.log(typeof true);
2console.log(typeof "true");
Console Output
boolean
string
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.
1console.log(Boolean("true"));
2console.log(Boolean("TRUE"));
3console.log(Boolean(0));
4console.log(Boolean(1));
5console.log(Boolean(''));
6console.log(Boolean('LaunchCode'));
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
1console.log(5 == 5);
2console.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
The ==
operator is one of six common comparison operators.
Operator | Description | Examples Returning true |
Examples Returning false |
---|---|---|---|
Equal (== ) |
Returns true if the two operands are equal, and false otherwise. |
|
|
Not equal(!= ) |
Returns true if the two operands are not equal, and false otherwise. |
|
|
Greater than (> ) |
Returns true if the left-hand operand is greater than the right-hand operand, and false otherwise. |
|
|
Less than (< ) |
Returns true if the left-hand operand is less than the right-hand operand, and false otherwise. |
|
|
Greater than or equal (>= ) |
Returns true if the left-hand operand is greater than or equal to the right-hand operand, and false otherwise. |
|
|
Less than or equal (<= ) |
Returns true if the left-hand operand is less than or equal to the right-hand operand, and false otherwise. |
|
|
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.
Question
Under which conditions does Boolean
convert a string to true
?
"true"
.false
.Question
Which of the following is a Boolean expression? Select all that apply.
3 == 4
3 + 4
3 + 4 === 7
"false"