본문 바로가기
TIL: Today I Learned

[TIL] 20201127 JavaScript Decision making

by 김알리 2020. 11. 27.

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
    1. Write your code in a .js file
    2. Include the script in a HTML file,  at the end of the <body>, right before </body>
    3. 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.