Logic
- The building blocks of interactivity
- Computers can do basic logical checks
- Greater than / greater than or equal to
- Less than / less than or equal to
- Equal to
- Not equal to
- These checks all turn out to be true or false
Less than
Greater than
Equal to
Not equal to
The if statement
-
Only runs a block of code if something is true
-
A block of code is anything inside of curly brackets { }
-
An example:
Else
- The else statement only runs if the thing within the if statement is false
Confirms
These are awful. Never use them in real development. And yet, for our purposes here, they can be useful.
In short, using the confirm method in JavaScript pops up a window that displays some text and lets the user respond with a yes or a no.
This response turns into a boolean true or false.
Nesting conditionals
- It is entirely possible to have if statements within if statements
Quick question
- With the code above, is there a chance the user will not see a final alert?
Logical operators
- The previous example can be simplified down a bit using logical operators
How the logical operators work
- looks for a true/false one either side
- Turns into a single true/false
Simplifying our previous if/else statement
Else if
Else ifs will only run if the previous if does not evaluate to true. They come after a closing curly bracket, and you can have also many as you want.
A brief note about formatting
- Note that every time a { appears, the next line is tabbed in
- And once that block exits with a }, the following lines are not longer tabbed in
- Get in the habit of doing this
- Yes, it will be worth points