In everyday programming, you may come across questions that need answers like:
- Yes or No
- On or Off
- True or False
- And pretty much anything else that has positive or negative answers.
In that case, you will need a data type called boolean
.
Boolean is the type of data like strings and numbers.
The difference is it can only have two values: true
and false
.
While boolean is simple to learn, it is so helpful that it is used everywhere in the programming world.
Use cases of boolean
We often use the boolean
type when we want to compare something or make a decision in the code.
Boolean in comparison operators
One of the uses of the boolean type is comparing something.
You can do so with the comparison operators when you want to compare two values and evaluate the results.
For example, if you want to check if Rick is older than John, you’ll do it with an operator Greater than >
:
let Rick = 32;
let John = 19;
let isOlder = Rick > John;
console.log(isOlder);
In the example above, JavaScript will return true
because number 32 is greater than number 19. Conversely, if you swap the “greater than” operator >
with “less than” <
, it will return false
because 32 is still greater than 19.
Boolean in if-else
statements
Another use case of boolean is controlling the flow of the program.
When you want the code to make decisions before running the program, you can do it with the if-else
statement.
In the next lesson, you’ll learn more about the if-else
statement, but here’s a quick intro.
The if-else
statement runs a code if a specified condition is true
. If the condition is false, another code will run.
It’s one of the main programming concepts like Variable.
For example, if you want to buy PlayStation 5 online, it will first check if you have enough money for it.
Here is an basic example of the if-else
statement:
let money = 800;
let cost = 499;
if (money > cost) {
console.log('You have enough money to purchase PlayStation 5');
} else {
console.log('You do not have enough money to purchase PlayStation 5');
}
In this example, you check whether the variable money stores value that’s bigger than the cost of the game console.
If it turns out to be true, the code inside the first set of the curly braces after the if
statement will run.
And if not, the execution will skip the first console log and move on to the second one in the second set of the curly braces after the else
statement.
In this specific case, since the value of the money
variable is greater than the value of the cost variable, the first condition is true
and so the first statement will run.
The output will display You have enough money to purchase PlayStation 5
and that means you can now buy PlayStation 5.
If you change the value of the money variable from 800
to 200
, the first condition will no longer be met, and so the second statement will run instead.
The output will display You do not have enough money to purchase PlayStation 5
meaning you cannot buy PlayStation 5 as you don’t have enough money.
Coding tasks
In this exercise, you will write a simple code that will display either true
or false
in the output. The first three tasks will involve adding a new code to get the desired output. In the fourth task, you will change the existing code to fix the problem.
And as usual, do not forget to hit the “RUN” button to run the program.
Task 1
Create a simple program that will check if the first
variable is less than the second
variable.
Type the following commands in the code editor:
let first = 57;
let second = 86;
let isLessThan = first < second;
console.log(isLessThan);
And hit the “Run” to run the program.
Task 2
A third variable called checker
has been created, but its values are empty. Add the code foodA == foodB;
to the checker
variable so that the output display will be true
.
let foodA = 'Pizza'
let foodB = 'Pizza'
let checker;
console.log(checker);
Task 3
Let’s try the task again, but this time we want the output to display false
. Change the value “Pizza” into “Burger” to the variable foodB
.
let foodA = 'Pizza'
let foodB = 'Pizza'
let checker = foodA == foodB;
console.log(checker);
Task 4
The output is currently displaying the second statement You can solve this task! Don't give up
because the value of the variable isLearning
has been set to false
.
let isLearning = false;
if (isLearning) {
console.log("You are doing a great job!");
} else {
console.log("You can solve this task! Don't give up");
}
Change the value of the variable isLearning
to true
so that the output returns You are doing a great job!
instead.