Decision Making with Code
- Boolean Logic : Having different outcomes depending on different criteria.
Comparison Operators
Has true or false output.
- > : greater than
- < : less than
- >= : greater than or equal to
- <= : leess than or equal to
- == : equality
- != : not equal
- === : strict equal
- !== : strict non-equal
Equality: Double vs. Triple Equals
- ==
- checks for equality of value, but not equality of type.
- It coerces both values to the same type and then compares them.
- This can lead to some unexpected results.
// == Examples
5 == 5; //true
'b' == 'c'; //false
7 == '7'; //true
0 == ''; //true
true == false; //false
0 == false; //true
null == undefined; //true
- ===
- checks for equality of value and type
- This is better to use than == most of the times.
// === Examples
5 === 5; //true
'b' === 'c'; //false
7 === '7'; //false
0 === ''; //false
true === false; //false
0 === false; //false
null === undefined; //false
Console
- console.log()
- Prints arguments to the console
Alert
- alert()
- Prints arguments to the user, but not in the console.
Prompt
- Prints arguments to the user, and accepts input from the user.
- Tip : change string into number
- parseInt(arg) : for integers
- parseFloat(arg) : for decimal numbers.
Running JavaScript From A Script
- How to run JavaScript from a script
- Write your code in a .js file
- Include the script in a HTML file, at the end of the <body>, right before </body>
- Inspect the HTML webpage to use console
If Statements
- if
- Only runs code if given condition is true
- else if
- Only runs code if the predeeding if statement is false and given condition is true
- else
- Only runs code if all the preceeding if & else if statements are false.
- If statements can be nested.
let rating = 1;
if (rating === 3) {
console.log("okay");
} else if {
console.log("cool");
} else {
console.log("meh");
}
Truth-y & False-y Values
Truth-y Values
- Everything else than "false-y" values are "truth-y"
False-y Values
- false
- 0
- "" (empty string)
- null
- undefined
- NaN
Logical Operators
- Logical AND (&&)
- Both sides must be true, for the entire thing to be true.
- Logical OR (||)
- If one side is true, the entire thing is true.
- Logical NOT (!)
- Returns true if expression is false.
!false //true
* This post is a summary of Udemy Course "The Web Developer Bootcamp" by Colt Steele.
'TIL: Today I Learned' 카테고리의 다른 글
[TIL] 20201201 JavaScript Object Literals (0) | 2020.12.01 |
---|---|
[TIL] 20201130 JavaScript Arrays (0) | 2020.12.01 |
[TIL] 20201126 JavaScript Strings and More (0) | 2020.11.27 |
[TIL] 20201125 JavaScript Basics (0) | 2020.11.25 |
[TIL] 20201124 CSS Frameworks: Bootstrap (0) | 2020.11.24 |