on
16. JavaScript Desicion Making
16. JavaScript Desicion Making
* Udemy "The Web Developer Bootcamp 2021"(by. Colt Steele)의 강의내용을 정리
161. Comparison Operators
- You can compare two different strings with comparison operators. Because all letters have numeric code as an ID, it is possible to compare two strings, althoguh it is not common.
'a' < 'b' // true 'A' > 'a' // false '@' <'A' // true
162. Equality: Triple Vs. Double Equals
- Double equals (==) : checks for equality of value, but not equality of type.
- Double equals coerces both values to the same type and then compares them.
- != also works in the same way with double equals.
1 == 1 // true 1 == '1' // true - unexpected result 0 == '' // true null == undefined // true 0 == false // true
- Triple equals (===) : checks for equality of value and type.
- !== also works in the same way with triple equals.
1 === 1 // true 1 === '1' // false 0 === false // false 10 != '10' // false 10 !== '10' // true
163. Console, Alert & Prompt
- console.log() prints arguments to the console, which is useful when we start working with the files.
- alert() prints arguments with pop-up message on the browser.
- prompt() prints an input pop-up on the browser, so that it can take a values from users.
165. If Statement
- If statements only run code if given condition is true.
function isEven(num) { if(num%2 === 0) { console.log ("even"} } isEven(2) // even isEven(3) // nothing happens - else is not required
166. Else-If
- If first condition was not in the business, we can use else if to add extra conditions.
- We can add multiple eles ifs.
167. Else
- If nothing was true, (including else-if...) try this one.
- Else-if and Else is not mandatory. Only the If is mandatory.
let testScore = 8 if (testScore < 5) { console.log("work harder") } else if (testScore > 8) { console.log("exellent") } else {console.log("not bad")}
168. Nesting Conditionals
- We can nest conditionals inside the other conditionals.
let password = "cat dog"; if (password.length >= 6) { if (password.indexOf('') !== -1) { console.log("password cannot include blank") } else {console.log("valid password"} } else {console.log("password is too short"}
169. Truthy and Falsy Values
- All JS values have an inherent truthyness or falsyness about them.
- Falsy values
false 0 "" (empty string) null undefined NaN
- Eveything else is truthy.
170. Logical AND
- Logical Operators allow us to combine different expressions.
- Logical AND(&&) : Both sides must be true.
1 <= 4 && 'a' === 'a'; // true 9 > 10 && 9 >= 9; // false 'abc'.length === 4 && 1 === '1' // false
171. Logical OR
- Logical OR(||) : If one side is true, it is true. It is basically works similarly with AND.
172. Logical NOT
- Logical NOT(!) : !expression returns true if expression is false.
! null // true ! (0 === 0) // false ! (3 <= 4) // false
173. The Switch Statement Is ... A Lot
- Switch Statement : control-flow statement that can replace multiple if statement.
- Keyword "break" is required to finish runtime if some cases are fulfilled.
- Keyword "default" is for a situation that no case is fulfilled.
const day 2; switch(day) { case 1: console.log("Monday") // prints out "Monday" only if day = 1 case 2: consele.log("Tuesday") case 3: consele.log("Wednsday") case 4: consele.log("Thursday") case 5: consele.log("Friday") default: console.log("I don't know that") // if day is not 1 ~ 5, it will print the string }
from http://oaat9309.tistory.com/33 by ccl(A) rewrite - 2021-11-08 18:27:25