Day 17 - JS types and comparisons
by Nino · 33 things on Twos
- Comparing Numbers
- to check if a number is less than or greater another number, we use the operators < and >
- 1 < 90
// true
- to check if a number is less than or equal to another number, we use the less-than-or-equal-to operator, <=
- to check if a number is greater than or equal to another number, we use the greater-than-or-equal-to operator, >=
- we can also use a comparison operator to compare a variable with another variable, like in min <= max
- const min = 5;
const max = 10;
const result = min <= max;
console.log(result);
// true
- ---
- Comparing Strings
- to check if a string is equal to another string, we can use the strict equality operator, === (see Day 17 list)
- ---
- Discovering Types
- values like booleans, strings, and numbers are called types
- ---
- Logical Operators
- the AND operator && returns true only if all the conditions are true
- let isBatteryOn = true;
let isSwitchOn = true;
console.log(isBatteryOn && isSwitchOn);
// true
- let isBatteryOn = false;
let isSwitchOn = false;
console.log(isBatteryOn && isSwitchOn);
// false
- i think it's called logical because all the conditions are true/false so the result should be true/false (?)
- and if the two values don't match it's false (?)
- let age = 18;
let isPass = true;
const isEligible = age >= 18 && isPass;
console.log(isEligible);
// true
- the result is true because age (18) is equal/greater than 18, which is true, then this true condition and the isPass (true) are both true 🤯
- we use the OR operator ||, which returns true as long as at least one of the conditions is true
- let isBatteryOn = true;
let isPowerOn = false;
console.log(isBatteryOn || isPowerOn);
// true
- we know that the NOT operator ! negates a boolean value. That means that it returns true if a condition is false and vice versa
- let isBulbOn = true;
console.log(!isBulbOn);
// false
- we can use ! to negate logical expressions as well. To do that, we place the logical expression between parentheses.
- let isBatteryOn = true;
let isPowerOn = false;
console.log(!(isBatteryOn && isPowerOn));
// true
- add lines
- Mimo: semi-completed (16. Types and Comparisons)
- insert node screenshot