JavaScript — Functions & Control Flow
Learn to make decisions in code with if/else, repeat actions with loops, and organize code into reusable functions.
What you'll learn
- Write functions with parameters and return values
- Use if/else for decision making
- Use for and while loops for repetition
- Understand comparison and logical operators
Playwright AutomationlessonsJump to another lesson
JavaScript --- Functions & Control Flow
Three big ideas: functions (reusable recipes), conditionals (decisions), and loops (repetition). Together they make code that thinks and acts.
What is a Function?
return "Hello, " + name
function greet(name) {
return "Hello, " + name + "!";
}
greet("Alice"); // "Hello, Alice!"
greet("Bob"); // "Hello, Bob!"
How a function flows
- 1
Input — parameters come in
function double(number) { - 2
Do stuff — body runs
const result = number * 2; - 3
Output — return sends a value back
return result; } - 4
Call it — use it anywhere
double(5); // 10
A function with no return returns undefined. Functions can take 0, 1, or many parameters.
return a + b
Arrow Functions --- The Shorter Way
Regular function
- Uses the function keyword
- Has its own name after function
- Return is always explicit
Arrow function
- Uses => arrow syntax
- Assigned to a const
- One-liner auto-returns (no curly braces needed)
// Regular
function isEven(n) {
return n % 2 === 0;
}
// Arrow (verbose)
const isEven = (n) => {
return n % 2 === 0;
};
// Arrow (one-liner — return is automatic)
const isEven = (n) => n % 2 === 0;
You’ll see arrow functions everywhere in Playwright code. Get comfortable reading them.
Quick check
What does this output? function addOne(n) { n + 1; // no return! } console.log(addOne(5));
Comparison Operators
| Operator | Meaning | Example | Result |
|---|---|---|---|
=== | exactly equal | 5 === 5 | true |
!== | not equal | 5 !== 3 | true |
> | greater than | 10 > 5 | true |
< | less than | 3 < 8 | true |
>= | at least | 5 >= 5 | true |
<= | at most | 3 <= 2 | false |
Every comparison returns a boolean (true / false).
if / else --- Making Decisions
const score = 75;
if (score >= 90) {
console.log("Grade: A");
} else if (score >= 70) {
console.log("Grade: C");
} else {
console.log("Grade: F");
}
// Grade: C
JavaScript checks conditions top-to-bottom and stops at the first true.
score = 75
score >= 90?
No
score >= 70?
Yes!
Grade: C
Logical Operators
Combine conditions with &&, ||, !.
// && (AND) — both must be true
if (age >= 18 && hasLicense) { /* can drive */ }
// || (OR) — at least one must be true
if (isWeekend || isHoliday) { /* day off! */ }
// ! (NOT) — flips true/false
if (!isLoggedIn) { /* please log in */ }
Loops --- Doing Things Repeatedly
for loop — run a set number of times
for (let i = 0; i < 3; i++) {
console.log(i);
}
# Output of the loop above012Try it in your terminal!
Three parts inside ():
- Start:
let i = 0(counter) - Condition:
i < 3(keep going while true) - Step:
i++(add 1 each time)
for...of loop — each item in a list
const colors = ["red", "green", "blue"];
for (const color of colors) {
console.log(color); // red, then green, then blue
}
while loop — run while a condition is true
let count = 0;
while (count < 3) {
console.log(count);
count++; // CRITICAL — without this, infinite loop!
}
Quick check
To check if two values are NOT equal (strict), use the operator.
Putting It Together
function gradeScores(scores) {
for (const score of scores) {
if (score >= 90) {
console.log(`${score}: A`);
} else if (score >= 70) {
console.log(`${score}: C`);
} else {
console.log(`${score}: F`);
}
}
}
gradeScores([95, 72, 58]);
// 95: A
// 72: C
// 58: F
Practice Time
Exercise 1: Write an If/Else and a Function
Fill in the blanks to complete this function that checks if someone can vote (must be 18 or older).
function canVote(age) { (age 18) { "Yes, you can vote!"; } { "Sorry, not old enough yet."; } } console.log(canVote(20)); // Yes, you can vote! console.log(canVote(15)); // Sorry, not old enough yet.
Exercise 2: Fill in the Blank
To check if two values are exactly equal (same value AND same type), use