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.
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.
5.1.1. Boolean Values¶
named after the British mathematician George Boole.
George Boole created Boolean Algebra, which is the basis of all modern computer arithmetic.
There are only two boolean values---
False are not valid boolean values.
1 2 3
console.log(true); console.log(typeof true); console.log(typeof false);
true boolean boolean
false are not strings. If you use quotes to
surround booleans (
"false"), those values become strings.
console.log(typeof true); console.log(typeof "true");
5.1.2. Boolean Conversion¶
As with the number and string data types, the boolean type also has a
Boolean. It works similarly to the
String functions, attempting to convert a non-boolean value to a boolean.
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
false. The equality operator,
==, compares two values and returns true
or false depending on whether the values are equal.
console.log(5 == 5); console.log(5 == 6);
In the first statement, the two operands are equal, so the expression evaluates
true. In the second statement, 5 is not equal to 6, so we get
We can also use
== to see that
"true" are not equal.
console.log(true == "true");
220.127.116.11. Comparison Operators¶
== operator is one of six common comparison operators.
|Greater than (
|Less than (
|Greater than or equal (
|Less than or equal (
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
=> 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 == 7 is
7 == a is also
true. However, an assignment statement is
a = 7 is legal while
7 = a is not.
If you explore the equality operator in more depth, you will find some
surprises. For example, the following comparisons return
7 == "7"
0 == false
0 == ''
We will explore the nuances of
== in the upcoming section
Equality, and introduce two new operators,
will align more closely with our intuitive notion of equality.
5.1.4. Check Your Understanding¶
Under which conditions does
Boolean convert a string to
- Only when the string is
- Whenever the string contains any non-whitespace character.
- Whenever the string is non-empty.
- Never. It converts all strings to
Which of the following is a Boolean expression? Select all that apply.
3 == 4
3 + 4
3 + 4 === 7