Booleans lesson
Booleans lessons project
Booleans:
Conecpt of booleans/Main idea
Booleans are a data type in JavaScript that represent one of two values: true or false. They are used for logical operations, comparisons, and control flow in programs.
What are Booleans:
• Booleans are simple data types that are either true or false.
• They are often used in conditional statements to execute specific blocks of code
commen use for Booleans:
• Conditional statements: if, else, structures
• Loops: while, do…while, and for loops
• Logical operations: combining conditions using && (AND), | (OR), and ! (NOT) |
Booleans expressions:
A Boolean expression is an expression that evaluates to true or false. Here are a few examples:
• 10 > 5 returns true because 10 is greater than 5.
• 8 === “8” returns false because the value is equal but the type is different (number vs. string).
• isLoggedIn = false; assigns false to the isLoggedIn variable.
Example:
%%js
let isUserLoggedIn = true;
let isAdmin = false;
if (isUserLoggedIn && isAdmin)
{console.log("Welcome, Admin!");}
else if (isUserLoggedIn)
{console.log("Welcome, User!");}
else console.log("Please log in.");
<IPython.core.display.Javascript object>
Popcorn Hack #1: Boolean Check:
Create Boolean variables to represent somthing about you
example:
• weather you birthday is today (isBirthday).
• Whether you have completed your chores (completedChores).
• Whether it is raining outside (isRaining). Use console.log() to display a sentence for each.
(you can use any representation, these are just examples)
Example:
%%js
let isBirthday = false;
let completedChores = true;
let isRaining = false;
console.log("Is today your birthday? " + isBirthday); // This will print "Is today your birthday? false"
console.log("Have you completed your chores? " + completedChores); // This will print "Have you completed your chores? true"
console.log("Is it raining outside? " + isRaining); // This will print "Is it raining outside? false"
<IPython.core.display.Javascript object>
%%js
let isHuman = false;
let addiction = false;
let dedication = true;
let isWorking = true;
let hasFreeTime = false;
console.log("Are you a human? " + isHuman); // This will print
console.log("Do you have an addiction? " + addiction); // This will print
console.log("Are you dedicated to your work? " + dedication); // This will print
// If-Else hell!
if (isHuman && dedication) {
console.log("You are a dedicated human.");
} else if (isHuman && !dedication) {
console.log("You are a human, but not dedicated.");
} else {
console.log("You are not a human.");
}
if (isWorking && dedication) {
console.log("You are working hard.");
} else if (isWorking && !dedication) {
console.log("You are working, but not dedicated.");
} else {
console.log("You are not working.");
}
if (hasFreeTime && !addiction) {
console.log("You have free time and no addiction.");
} else if (hasFreeTime && addiction) {
console.log("You have free time but have an addiction.");
} else {
console.log("You do not have free time.");
}