Numbers are a type of data. There are lots of other data types (like undefined). The next data type we'll introduce is called boolean. A boolean has two possible values, true and false.
Try evaluating true
in the console. You'll get true
. Try assigning the value false
to a variable, like let iHatePizza = false
. Then evaluate iHatePizza
, and you'll see that, that is completely untrue i.e. false
.
How are booleans useful to us?
Well, there's a type of operator called a ternary, the ternary operator lets you write conditional expressions. Here are a couple examples of ternaries in action:
true ? 1 : 0
produces1
false ? 1 : 0
produces0
The structure of the conditional expression is basically: boolean ? lefthandExpression : righthandExpression. So like true ? 1 : 0
. the lefthand expression is evaluated if the condition is true, otherwise the righthand expression is evaluated. If we wrote it in English it would be like: if condition then this expression otherwise this other expression.
We can assign the conditional expression to a variable like any other expression.
// let's organize a party! What food do we need?
let numberOfGuests = 12
let weLovePizza = true
let numberOfPizzas = weLovePizza ? numberOfGuests / 4 : 0
let numberOfSandwiches = numberOfGuests * 2 - numberOfPizzas * 2
If we evaluate the above code, then evaluate numberOfSandwiches
we'll get back 12
. If we evaluate numberOfPizzas
we'll get back 3
.
So if we have 12 guests, and they like pizza, then we should order 12 sandwiches and 3 pizzas.
What if our guests don't like pizza?
numberOfGuests = 12
weLovePizza = false
numberOfPizzas = weLovePizza ? numberOfGuests / 4 : 0
numberOfSandwiches = numberOfGuests * 2 - numberOfPizzas * 2
Now numberOfPizzas
is 0
and numberOfSandwiches
is 24
.
A boolean is a data type that has two possible values, true and false. We can use comparison operators to create booleans by comparing other things, like numbers.
operator | what it does | example |
---|---|---|
> |
greater than | 2 > 1 |
< |
less than | 1 < 2 |
>= |
greater than or equal to | 2 >= 1 |
<= |
less than or equal to | 1 <= 2 |
== |
equal to | 2 == 2 |
!= |
not equal to | 2 != 1 |
=== |
strict equal to | 2 === 2 |
!== |
strict not equal to | 2 !== 1 |
All of the examples in the table above evaluate to true. So 2 > 1
(2 is greater than 1) is true
, and 2 < 1
(2 is less than 1) is false
.
Booleans can be combined to produce new boolean values.
operator | what it does | example |
---|---|---|
&& | AND | true && true |
|| | OR | true || false |
! | NOT | !false |
All of the examples in the table above evaluate to true.
Boolean logic is its own thing, but here's a short intro:
- AND means if both the left and right sides are true, the expression is true otherwise the expression is false.
- OR means that if either or both the left and right sides are true the expression is true. If neither is true, the expression is false.
- NOT negates the value so that true becomes false, and false becomes true.
Of course comparison operators and logical operators are usually combined in an expression to create a boolean value, like this 2 > 1 && -1 < 0
(2 is greater than 1 and -1 is less than 0), which is true
.
Think through these expressions to decide what value is produced, and then double check in the console:
-
true || false
-
false && false
-
1 < 2 && 2 > 1
-
31 < 13 || 1 < 2 && 3 > 1
-
400 <= 400 && 399 < 400 && (30 > 31 || 400 > 31)
-
true && false && false || false && true
-
true && false || true || false
-
true || false ? true : false
-
true && false && false || false && true ? true && false && false || false && true : 1 < 2 && 2 > 1
// a function for saying whether a number is 0 or not
let isZero = (num) => num === 0
isZero(3) // false
isZero(0) // true
isZero() // false
// a function to return the absolute value of a number
let abs = (num) => num < 0 ? num * -1 : num
abs(13) // 13
abs(-13) // 13
// a function that takes a number and enforces a range on it
// returning the limits of the range if the number is outside
let betwixt = (num) => num > 100 ? 100 : num < 0 ? 0 : num
betwixt(56) // 56
betwixt(17) // 17
betwixt(-39) // 0
betwixt(176) // 100
- In the callbacks module we modelled finances. Can you add a lottery ticket to finances by using booleans and random numbers?