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 isCloudy = true;
let isSunday = false;
let isCompSci = true;
console.log("Is it cloudy today? " + isCloudy);
console.log("Is it Sunday today? " + isSunday);
console.log("Are we in computer science right now? " + isCompSci);