Booleans
Booleans are the "true" and "false" values. They are a fundamental piece of any programming language and they are used to control all the logic of our programs.
Booleans are created when we use comparison operators:
| Operator | Description |
|---|---|
| > | Greater than |
| >= | Greater than or equal to |
| < | Less than |
| <= | Less than or equal to |
| == | Equal to |
| === | Identical (same type and value) |
| != | Not equal to |
| !== | Not equal value or not equal type |
20 > 2; // true
2 >= 2; // true
5 < 5; // false
5 <= 10; // true
20 == 20; // true
'john' == 'johnny' // false
'a' == 'A' // false
20 === "20"; // false - same value but different types
20 != 10; // true
10 != 10; // false
"10" !== 10; // true - type is different
/*
Be careful when using greater than and less than operators with different data types.
That can lead to unexpected results
*/
We'll use booleans on conditional structures. Let's say you will offer finance options for you customers on orders of US$1000 or above:
var orderValue = 2800;
if( orderValue >= 1000 ) {
console.log("You are eligible to finance your order.");
} else {
console.log("Finance options are not available for orders below US$1000");
}
We'll explore conditionals in depth in the next few lessons.