Program Flow: Conditional Statements
Learn how to execute code selectively based on predefined conditions, providing flexibility and control over program flow using conditional statements.
We'll cover the following...
Conditional statements are another way we can control the flow of our program. They allow us to execute code only under certain conditions that we define. Conditional statements are incredibly useful for defining exactly under what circumstances a block of code will run.
The if
statement
Let’s start with an example. Given the variable temperature
, we want to inform a user about weather conditions.
Press + to interact
var weather = function(temperature){console.log("The temperature outside is", temperature, "degrees fahrenheit.");}weather(60);
Let’s say we want to go beyond just giving the temperature
value and want to inform users on whether or not they should wear a jacket. How could this be done ...