# Idea ## if else if else ```js const time = 15; let greeting; if (time < 10) { greeting = "Good morning"; } else if (time < 20) { greeting = "Good day"; } else { greeting = "Good evening"; } ``` ## Comparison operators - `<`: less than - `>`: greater than - `<=`, `>=` - `===`: is equal to (identity operator; equivalence)^[https://discuss.codecademy.com/t/whats-the-different-between-two-and-three-equal-signs/505767] - data types must also be equal - value must be equal - `==`: equality (not strict) - only value is equal - data types can be different - `!==`: not equal to ## Logical operators - `&&`: and - `||`: or - `!`: not ## Truthy and falsy The following if code block runs even though `some_val` is not `true`. It has a **truthy** value. That is, it has a **non-falsy** value. ```js const some_val = 15; if (some_val) { console.log('truthy value'); } ``` Falsy values - `0`, `""`, `''`, `null`, `undefined`, `NaN` Truthy and falsy evaluations allow for short-hand notations. ```js // verbose let username; let defaultName; if (username) { defaultName = username; } else { defaultName = 'Stranger'; } console.log(defaultName); // 'Stranger' ``` JavaScript assigns the truthy value to a variable if you use the `|| ` operator in your assignment. It checks the left-hand condition first, then the next one, and so on. This notation is known as [[javascript - short-circuit evaluation]]. ```js // short-hand let username; let defaultName = username || 'Stranger'; console.log(defaultName); // 'Stranger' let username; let name = 'John'; let defaultName = username || name || 'Stranger'; console.log(defaultName); let tool = ''; let writingUtensil = tool || 'pen'; console.log(`The ${writingUtensil} is mightier than the sword.`); let tool = 'marker'; let writingUtensil = tool || 'pen'; console.log(`The ${writingUtensil} is mightier than the sword.`); ``` ## Ternary operator It simplifies an `if...else` statement: ``` condition ? evaluate if true : evaluate if false ``` ```js if (isCorrect) { console.log('Correct!'); } else { console.log('Incorrect!'); } isCorrect ? console.log('Correct!') : console.log('Incorrect!'); ``` ## Switch keyword ```js let groceryItem = 'papaya'; if (groceryItem === 'tomato') { console.log('Tomatoes are $0.49'); } else if (groceryItem === 'papaya'){ console.log('Papayas are $1.29'); } else { console.log('Invalid item'); } // the value in switch will be compared with each value in case let groceryItem = 'papaya'; switch (groceryItem) { case 'tomato': console.log('Tomatoes are $0.49'); break; case 'lime': console.log('Limes are $1.49'); break; case 'papaya': console.log('Papayas are $1.29'); break; default: console.log('Invalid item'); break; } ``` # References - [codecademy - short circuit evaluation](https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-conditionals/lessons/control-flow/exercises/truthy-falsy-operators) - [codecademy - ternary operator](https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-learn-javascript-syntax-conditionals/lessons/control-flow/exercises/ternary-operator)