1.4 Boolean¶
True or False¶
In x shop, customers get a 50% discount if their order is over $100. A customer, named Soso, bought some products for $150. Let's try to test if Soso will get the discount or not.
var sosoCanGetDiscount = 150 > 100;
console.log(sosoCanGetDiscount);
You will get: true.
What does that mean? It means yes, Soso will get the discount. Why?! because 150 is greater than 100. Let's test the same thing for Sami. Sami bought a product for $50.
var samiCanGetDiscount = 50 > 100;
console.log(samiCanGetDiscount);
You will get: false.
What does that mean? It means no, Sami will NOT get the discount. Why?! because 50 is NOT greater than 100. true means that the condition you are testing satisfies. false means that the condition you are testing does NOT satisfy.
Exercises¶
Task 1: Read this code and try to understand it.
var thirtyLessThanEighty = 30 < 80;
console.log(thirtyLessThanEighty);
You will get: true
Task 2: Read this code and try to understand it.
var thirtyLessThanEighty = 30 < 80;
console.log(typeof thirtyLessThanEighty);
You will get: boolean.
What is typeof
? typeof
gives us the data type that the variable contains. Wait! what is the data type? Let's investigate it!
Task 3: Read this code and try to understand it.
var word = "Hello";
console.log(typeof word);
You will get: string.
Task 4: Read this code and try to understand it.
var num = 10;
console.log(typeof num);
You will get: number.
Task 5: Read this code and try to understand it.
var bool1 = true;
console.log(typeof bool1);
You will get: boolean.
Task 6: Read this code and try to understand it.
var bool2 = false;
console.log(typeof bool2);
You will get: boolean.
typeof
helps us to know what is the type of the variable value.
Task 7: Check if the provided name is equal to "Ali". Hint: Use ==
.
var name1 = "Ali";
var name2 = "Ola";
console.log(name1 == "Ali");
console.log(name2 == "Ali");
The result will be:
true
false
Task 8: Check if someone's temperature is greater than or equal to 37.5. Hint: use >=
.
var temp1 = 39;
console.log(temp1 >= 37.5);
var temp2 = 37.5;
console.log(temp2 >= 37.5);
var temp3 = 37;
console.log(temp3 >= 37.5);
The result will be:
true
true
false
Task 9: Check if the room temperature is less than or equal to 25. Hint: use <=
.
var temp1 = 25;
console.log(temp1 <= 25);
var temp2 = 30;
console.log(temp2 <= 25);
var temp3 = 0;
console.log(temp3 <= 25);
The result will be:
true
false
true
Task 10: Check if the thing, the kid bought, is NOT equal to ice cream. Hint: use !=
.
var kidBuys = "bread";
console.log(kidBuys != "ice cream");
var kidBuys = "ice cream";
console.log(kidBuys != "ice cream");
The result will be:
true
false
Task 11: Test the following code, and try to understand it.
var num = "5";
console.log(5 == num);
var num = "5";
console.log(5 === num);
The result will be:
true
false
What did happen here? Let's check the typeof num
:
var num = "5";
console.log(typeof num);
You will get: string
What is the typeof 5
?
console.log(typeof 5);
You will get number
num == 5
evaluates to true because the value of num
is equal to the value of 5. num === 5
evaluates to false, why? Although the value of num
is equal to 5, the type of num
(string) is NOT equal to the type of 5
(number).
Task 12: Check if the value and the type of the variable testVar = "555"
is NOT equal to 555. Hint: use !==
.
var testVar = "555";
console.log(testVar !== 555);
You will get: true
Project: The discount again¶
- A shop offers a 50% discount for orders over $100.
- Display "Customer's name got the discount: true" for the customers who got the discount.
- Display "Customer's name got the discount: false" for the customers who did not get the discount.
- A customer, named Soso, bought some products for $150.
- Sami bought a product for $50.
var customer1 = "Soso";
var customer1GetsDiscount = 150 >= 100;
console.log(customer1 + " got the discount: " + customer1GetsDiscount );
var customer2 = "Sami";
var customer2GetsDiscount = 50 >= 100;
console.log(customer2 + " got the discount: " + customer2GetsDiscount );
The result will be:
Soso got the discount: true
Sami got the discount: false