Conditions
Last updated on
You are constantly using conditional statements in your everyday life. Is the water boiling in the pot? If it is, put in the pasta. Otherwise, wait a few more minutes. 🧑🍳
It’s the same in your code. Depending on the data, you can instruct your program or algorithm to act differently.
Note that I assume you’ve completed the previous lessons in Chapter 1, TypeScript fundamentals, especially the Setup lesson, before diving into this one.
Setup
Create a new, empty folder on your computer, open it with VS Code, and create a file named main.ts inside it.
Type this code in main.ts and save the file:
console.log("Hello!");Open the terminal (Command + J on Mac or Control + J on Windows) and run this command:
node --watch main.tsThis runs our code and automatically runs it again each time we save a change, so we can see the results as we work.

Conditional statements
To write conditions, we use the conditional statements if, else if, and else.
The first condition should always be tested with if. The condition should be enclosed in (). If the condition is met, the computer will execute the code inside the following {}.
For example, the code below logs I am older than you! in the terminal only if myAge is greater than yourAge.
const myAge = 36;
const yourAge = 25;
if (myAge > yourAge) {
console.log("I am older than you!");
}Since 36 is greater than 25, the condition is met and the message appears in the terminal.

Now change yourAge to 55 and save the file:
const myAge = 36;
const yourAge = 55;
if (myAge > yourAge) {
console.log("I am older than you!");
}Now myAge is not greater than yourAge, so console.log is skipped and no message is logged in the terminal.

If we want to test more than one condition, we use else if with a condition in () followed by code to be executed inside {}.
In the code example below, we also want to log You are older than me! if myAge is smaller than yourAge.
const myAge = 36;
const yourAge = 55;
if (myAge > yourAge) {
console.log("I am older than you!");
} else if (myAge < yourAge) {
console.log("You are older than me!");
}If you run this code, you’ll now see You are older than me! in the terminal.

But what happens if myAge and yourAge are exactly the same? Set both ages to 36 and save:
const myAge = 36;
const yourAge = 36;
if (myAge > yourAge) {
console.log("I am older than you!");
} else if (myAge < yourAge) {
console.log("You are older than me!");
}Nothing is logged again: our conditions only cover ages that are greater or smaller, not equal.

If we want to run some code when none of the conditions are met, we use else. Since else doesn’t test any condition, we don’t need (), but we must provide the code to be executed inside {}. Although finishing with an else isn’t mandatory, it often helps prevent unexpected bugs.
For example, in the code below, if none of the conditions are met, it means that we are the same age, so we use else to log We are the same age! in the terminal.
const myAge = 36;
const yourAge = 36;
if (myAge > yourAge) {
console.log("I am older than you!");
} else if (myAge < yourAge) {
console.log("You are older than me!");
} else {
console.log("We are the same age!");
}
Have you noticed the absence of ; on line 10? It’s because when you use {}, you create a block of code. The closing curly bracket already tells the computer that it’s the end of the block, so there’s no need for an extra semicolon at the end.
Comparison operators
When using if and else if, the condition inside the () should always result in a boolean value (true or false).
The previous example works because we used the comparison operators > and <, which return a boolean.
Here are the main comparison operators you’ll use in your daily work:
>: greater than>=: greater than or equal to<: less than<=: less than or equal to===: equal to!==: not equal to
You might see examples around the web with == and !=. These operators can automatically convert values before comparing them, which can produce unexpected results. It’s best to stick with === and !==, which compare without converting types.
And here are the operators in action. We store the results in variables and log them:
// Change the ages to test the different operators.
// Don't worry about the `: number` for now.
const myAge: number = 36;
const yourAge: number = 25;
const greater = myAge > yourAge;
console.log("greater =>", greater);
const greaterOrEqual = myAge >= yourAge;
console.log("greaterOrEqual =>", greaterOrEqual);
const younger = myAge < yourAge;
console.log("younger =>", younger);
const youngerOrEqual = myAge <= yourAge;
console.log("youngerOrEqual =>", youngerOrEqual);
const sameAge = myAge === yourAge;
console.log("sameAge =>", sameAge);
const differentAge = myAge !== yourAge;
console.log("differentAge =>", differentAge);
Logical operators
Sometimes, you want to check multiple conditions in one statement. This is where logical operators && and || are useful.
&&means AND||means OR
For example, let’s say we need an algorithm to check if you are old enough to have a drink in a bar in Quebec (at least 18) or Ontario (at least 19). 🥂🇨🇦
Without logical operators, it could look like this, with nested conditions. It works, but nested conditions can be difficult to read and debug.
// Change the age and province to test the conditions.
// Don't worry about the `: number` and `: string` for now.
const myAge: number = 18;
const myProvince: string = "Quebec";
if (myProvince === "Quebec") {
// Inside the province check, we also check the age.
if (myAge >= 18) {
console.log("You are allowed! 🍺");
} else {
console.log("You are not allowed! 🚫");
}
} else if (myProvince === "Ontario") {
if (myAge >= 19) {
console.log("You are allowed! 🍺");
} else {
console.log("You are not allowed! 🚫");
}
} else {
console.log("I don't know this province! 🤷");
}
Here’s the same code with the logical operator && to make sure myAge AND myProvince meet specific conditions. This approach is simpler to understand and easier to debug!
// Change the age and province to test the conditions.
// Don't worry about the `: number` and `: string` for now.
const myAge: number = 18;
const myProvince: string = "Quebec";
if (myProvince === "Quebec" && myAge >= 18) {
// Quebec AND at least 18.
console.log("You are allowed! 🍺");
} else if (myProvince === "Ontario" && myAge >= 19) {
// Ontario AND at least 19.
console.log("You are allowed! 🍺");
} else if (myProvince !== "Quebec" && myProvince !== "Ontario") {
// The province is neither Quebec nor Ontario.
console.log("I don't know this province! 🤷");
} else {
console.log("You are not allowed! 🚫");
}We could also write the same algorithm with a mix of && and ||. Additionally, we can use variables to simplify the code.
// Change the age and province to test the conditions.
// Don't worry about the `: number` and `: string` for now.
const myAge: number = 18;
const myProvince: string = "Quebec";
// Store each combined condition as true or false.
const isAllowedInQuebec = myProvince === "Quebec" && myAge >= 18;
const isAllowedInOntario = myProvince === "Ontario" && myAge >= 19;
const unknownProvince = myProvince !== "Quebec" && myProvince !== "Ontario";
if (isAllowedInQuebec || isAllowedInOntario) {
console.log("You are allowed! 🍺");
} else if (unknownProvince) {
console.log("I don't know this province! 🤷");
} else {
console.log("You are not allowed! 🚫");
}Just like in English, there are different ways to express the same logic in code. Keep it easy to read and understand so you can return to it months later and easily pick up where you left off, and your colleagues can quickly understand and review your work.
Conditional operator
There’s another way to write simple conditional statements using the condition ? valueWhenTrue : valueWhenFalse syntax. This syntax is called the conditional operator (also known as the ternary operator).
It’s great for simple conditions, allowing you to replace if and else statements with a single line.
Here’s another variation of our algorithm using this syntax.
// Change the age and province to test the conditions.
// Don't worry about the `: number` and `: string` for now.
const myAge: number = 18;
const myProvince: string = "Quebec";
const quebecMessage = myAge >= 18
? "You are allowed! 🍺"
: "You are not allowed! 🚫";
const ontarioMessage = myAge >= 19
? "You are allowed! 🍺"
: "You are not allowed! 🚫";
if (myProvince === "Quebec") {
console.log(quebecMessage);
} else if (myProvince === "Ontario") {
console.log(ontarioMessage);
} else {
console.log("I don't know this province! 🤷");
}Conclusion
All of the algorithms discussed above for checking if the user can order a drink will produce the same results. But which one is THE BEST?
Well, it really depends on the context and your personal preferences.
Remember, you are learning a programming language. And just like in any language, there are many ways to express the same idea. As long as your computer and colleagues can understand you, you’ll be fine! 🤖
Now, it’s time for the next lesson. Let’s discover loops!