Loading lesson path
Concept visual
Start at both ends
In JavaScript, a Boolean is a primitive data type that can only have one of two values: true or false The Boolean value of an expression is the basis for all JavaScript comparisons and conditions. Key Boolean Characteristics true and false are boolean data types true and false are the only possible boolean values true and false must be written in lowercase true and false must be written without quotes
Very often, in programming, you will need a data type that can represent one of two values, like: yes or no on or off true or false Boolean values are fundamental for logical operations and control flow in JavaScript programming.
All JavaScript comparison operators (like ==, !=, <, >)
return true or false from the comparison.Formula
Given that x = 5, the table below explains comparison:(x == 8) false
(x != 8) true
Formula
(x > 8)false
Formula
(x < 8)true
let x = 5;
(x == 8); // equals false
(x != 8); // equals trueBooleans are extensively used in if statements to determine the code blocks to execute based on the logic.
Result if (day == "Monday")
true or false if (salary > 9000)
true or false if (age < 18)true or false
Example if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}Booleans are extensively used in loops to determine conditions for looping.
For loop for (i = 0; i < 5; i++)
While loop while (i < 10)
For in loop for (x in person)
For of loop for (x of cars)Example while (i < 10) {
text += i;
i++;
}