What Is a Control Structure?
A control structure is a way to tell your program to do something only if a certain condition is met. Think of it like a switch: the wire (your variable) is connected to a light only if a specific condition is true. If the condition isn’t met, the light stays off.
The if Statement
The simplest control structure in JavaScript is the if statement. It checks a condition, and if that condition is true, the code inside the if block runs.
For example:
if (number === 10) {
console.log("Number is 10");
}
Notice we use the strict equality operator (===). As you remember from the previous module, this operator checks that the value is exactly equal to 10 (its data type and value both match).
The if-else Statement
Sometimes, you want your program to do one thing when a condition is true, and something else when it isn’t. That’s where the if-else statement comes in. It works like this:
if (condition) {
// Code if the condition is true
} else {
// Code if the condition is false
}
For instance, you might check if someone is old enough to vote:
let age = 16;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
Exercises
Basic if Statement
Create a variable named number with the value 10 using let.
Write an if statement that checks if number is equal to 10.
If the condition is true, print "Number is 10" using console.log.
if-else Statement
- Create a variable named age with the value 16 using let.
- Write an if-else statement that checks if age is greater than or equal to 18.
- If the condition is true, print "Adult".
- Otherwise, print "Minor".


Ready to become an ethical hacker?
Start today.
As a member of Hakatemia you get unlimited access to Hakatemia modules, exercises and tools, and you get access to the Hakatemia Discord channel where you can ask for help from both instructors and other Hakatemia members.