Welcome to the world of truth or falsehood! This chapter dives deep into booleans, the fundamental building blocks of logical reasoning in JavaScript. We'll explore their role in decision-making, conditional statements, and how they interact with other data types. By the end, you'll be a master of boolean logic in your JavaScript programs.
true or false.You can explicitly assign true or false to a variable:
let isLoggedIn = true;
let isNightTime = false;
Booleans shine in conditional statements like if, else if, and else:
if (isLoggedIn) {
console.log("Welcome back!");
} else {
console.log("Please log in.");
}
if statement checks if isLoggedIn is true.if block executes (prints “Welcome back!”).isLoggedIn is false, the else block executes (prints “Please log in.”).We use comparison operators (==, !=, ===, !==, <, >, <=, >=) to compare values and generate boolean results:
const age = 25;
const isAdult = age >= 18; // True (age is 25, which is greater than or equal to 18)
const password = "secret123";
const isCorrectPassword = password === "secret123"; // True (strict comparison: value and type must match)
JavaScript sometimes converts other data types to booleans in certain contexts:
true.0), NaN (Not a Number), and -Infinity are considered false."") is considered false.true.null and undefined:false.true.[]) or object ({}) is considered false.Booleans can be combined using logical operators (&& – AND, || – OR, ! – NOT) to create more complex conditions:
const hasPermission = true;
const isWeekend = false;
if (hasPermission && !isWeekend) { // AND: both conditions must be true
console.log("You can access the resource.");
} else {
console.log("Access denied.");
}
const hasCoupon = false;
const isMember = true;
if (hasCoupon || isMember) { // OR: at least one condition must be true
console.log("You qualify for a discount!");
} else {
console.log("No discount available.");
}
const hasPermission = true;: This declares a constant variable named hasPermission and assigns it the boolean value true.const isWeekend = false;: This declares another constant variable named isWeekend and assigns it the boolean value false.const hasCoupon = false;: This declares a constant variable named hasCoupon and assigns it the boolean value false.const isMember = true;: This declares a constant variable named isMember and assigns it the boolean value true.if (hasPermission && !isWeekend) { ... }: This checks for two conditions using the AND operator (&&):hasPermission: This condition is true.!isWeekend: This applies the NOT operator (!) to isWeekend, effectively checking if it’s not the weekend. Since isWeekend is false, !isWeekend is true.true for the code within the if block to execute. In this case, they are both true, so the code proceeds:console.log("You can access the resource.");: This prints the message “You can access the resource.” to the console.if (hasCoupon || isMember) { ... }: This checks for two conditions using the OR operator (||):hasCoupon: This condition is false.isMember: This condition is true.true for the code within the if block to execute. In this case, isMember is true, so the code proceeds:console.log("You qualify for a discount!");: This prints the message “You qualify for a discount!” to the console.The code effectively implements two conditional checks:
true or false values.&&, ||, !) combine boolean values to create complex conditions.if, else) control code execution based on boolean conditions.Booleans are the foundation of logical decision-making in JavaScript. By mastering their core concepts, comparison operators, and logical combinations, you can write well-structured and dynamic programs that respond based on various conditions. Remember, booleans offer a simple yet powerful way to control the flow of your code! Happy coding !❤️
