Booleans

The simplest data type!

Named after this guy

George Boole, mid 19th century English mathematician and logician

Only two values

true & false

Can also think of them as

yes & no

on & off

etc.

Used with if

if (<boolean>) {
  // do something
}

Yeah, we haven’t learned about if yet.

… and with while

while (<boolean>) {
   // do something
}

Haven’t learned about while yet, either.

We’ll get to it.

But the key thing is

They are just another kind of value.

So we can ask our usual questons about values

  1. What values can be represented?

  2. What is the syntax for writing them in our langauge?

  3. What can we do with them?

What values can be represented?

As we said before, true and false.

How are they represented in the computer?

It doesn’t matter.

I literally do not know.

They are fully abstracted.

How do we write booleans?

true

false

That’s it.

Unlike numbers, there aren’t different ways of writing the same literal value.

What can we do with booleans?

Same as with numbers: combine them in expressions.

But the operators are different.

Three logicial operators

AND, OR, and NOT

These are the three operators defined in Boolean Algrebra by our friend George Boole.

And

Writen &&

true && false

Evaluates to true if, and only if, both of its operands are true.

» true && true
true

» true && false
false

» false && true
false

» false && false
false

Or

Written ||

true || false

Evaluates to true if either, or both, operands are true.

» true || true
true

» true || false
true

» false || true
true

» false || false
false

Not

Written !

!true

Flips the logical value, true to false and false to true.

» !true
false

» !false
true

Writing expressions with all literal values (true and false) is kinda silly because we could just figure out what the value is and write that.

But they make a lot more sense when we are writing expressions in terms of named values.

We’ll talk a lot more about variables later.

For now just know that we can use a name to refer to a value in the place of a literal value.

E.g.

happy = true

Some examples.

Am I hangry?

Suppose hungry is a boolean that says whether I’m hungry and angry says whether I’m angry.

What’s an expression that captures whether or not I’m hangry?

hungry && angry

Do I stay up late?

Suppose homework is a boolean that says whether I have homework to grade and newEpisodes is a boolean that says whethere there new episodes of my favorite TV show available.

What’s an expression that captures whether I will stay up late if I always stay up late to grade homework or to watch new episodes of my favorite show?

homework || newEpisodes

Am I awake?

asleep is a boolean that says whether I’m asleep.

What’s an expression that says whether I’m awake?

!asleep

Operators on other types that produce boolean values

Equality comparisons

=== !==

» 1 === 2
false

» 1 !== 2
true

Ordering comparisons

< > <= >=

» 10 < 20
true

» 10 < 10
false

» 10 <= 10
true

» 10 <= (11 - 1)
true

» 100 >= 200
false

You can use the equality operators on booleans

But it’s very rarely needed.

» true === false
false

» true !== false
true

And definitely don’t compare the value of a boolean expression to a boolean literal.

Instead of

x === true

just write:

x

Otherwise why not write?

(x === true) === true

Or maybe?

((x === true) === true) === true

🤔

And instead of

x === false

or

x !== true

just write:

!x

tl;dr

Booleans are just another kind of value.

Up next

Strings