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.
boolean, 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.
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");
As with the number and string data types, the boolean type also has a conversion function,
Boolean. It works similarly to the
String functions, attempting to convert a non-boolean value to a boolean.
Boolean converts various non-boolean values.
console.log(Boolean("true")); console.log(Boolean("TRUE")); console.log(Boolean(0)); console.log(Boolean(1)); console.log(Boolean('')); console.log(Boolean('LaunchCode'));
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 to
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");
== 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 |
=) instead of a double equal sign (
==). Remember that
= is an assignment operator and
== is a comparison operator. Also note that
=> 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 not symmetric:
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
, and introduce two new operators,
!==, that will align more closely with our intuitive notion of equality.
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