IF Statements

IF Statements

In Javascript IF statments are used to execute a block of code if a specified condition is true, you would use "if". Then if you want to specify a block of code for the same condition but use the false value you would use "else" Also you can use "else if" to specify a new condition to test, if the first condition is false.

if (condition1) {
  block of code to be executed if the condition1 is true
} else if (condition2){
  block of code to be executed if the condition1 is false and condition2 is true. 
} else {
  block of code to be executed if the condition1 is false and condition2 is false. 
}
                

That is an example of the proper syntax to use for If statements

Below is a example of an IF statmente following the syntax from above

const input = prompt("Do you like veggies (y/n)");

if(input == "y"){
    console.log("I recommend the pasta salad.");
}else if(input == "n"){
    console.log("I recommend the prime rib");
}else{
    console.log("Invalid input")
}
                

In this example the first step is to declare the varible of input and send out a prompt to the user. Then you are going to set "y" as condition1 and you put "==" to see if the two values are equal. Then you are going console.log a response to the prompt you sent. Next you are going to set "n" as condition2 and you put "==" to see if the two values are equal. After that you are going to console.log a response bases on the promt you sent. Finally you are going to add a fail safe if the user answer anything else than you have in condition 1 and 2. So you add an else statent and console.log saying something like "Invalid Input".