For the JavaScript review, let’s work in the console, so have that open.
Conditional Statements
Conditional statements allow your code to perform certain commands only if certain conditions are met. These conditions can be based on:
a user’s input(is the password entered correct?)
the current state(is it day or night?)
the value of a certain element(is this person older than 18?)
Example, try this in the console:
if (newDate().getHours() < 18) {
console.log("The sun is out! Rejoice!");
}
In this example we used the operator < or, less than. It’s checking to see if the current hours are less than 18(which in military time is 6pm).
Last week we looked at a lot of different operators, here they are again:
==
check is this equal to(2=="2" would be true, because the string gets converted to a number)
===
equal value and equal type(2==="2" would be false, because the second 2 is a string)
!=
not equal
!==
not equal value or not equal type
>
greater than
<
less than
>=
greater than or equal to
<=
less than or equal to
Practice
Create a comparison that does the following: if the time isn’t equal to 12, log“Not lunch time”
If/Else Statements
The above examples are great, but how can we be a little more specific? You can use if/else statements to add more specificities. There are a few different variations of this, here are the correct syntaxes of each.
// If statement, what we did before
if ( condition ) {
// block of code to be executed if the condition istrue
}
// Else statement
if ( condition ) {
// block of code to be executed if the condition istrue
} else {
// block of code to be executed if the condition isfalse
}
// Else if statement
if ( condition1 ) {
// block of code to be executed if condition1 istrue
} elseif ( condition2 ) {
// block of code to be executed if the condition1 isfalseand condition2 istrue
JavaScript Review
Conditional Statements
if (new Date().getHours() < 18) {
console.log("The sun is out! Rejoice!");
}
If/Else Statements
// If statement, what we did before
if ( condition ) {
// block of code to be executed if the condition is true
}
// Else statement
if ( condition ) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
// Else if statement
if ( condition1 ) {
// block of code to be executed if condition1 is true
} else if ( condition2 ) {
// block of code to be executed if the condition1 is false and condition2 is true