TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
JavaScript / Chapter 7 of 60~12 min • Beginner
Chapter 07JavaScript

Conditional statements

An if statement chooses a block based on a condition. else if adds alternatives and else handles all remaining cases. Conditions can combine comparisons. In tests, do not hide failed assertions by branching around them.

How it works

Step-by-step walkthrough

  1. Set a status code
  2. Compare against an expected value
  3. Print the outcome

Example

JavaScript
const status = 200;
if (status === 200) {
  console.log('Success');
} else {
  console.log('Unexpected status');
}
Expected result: Success
💡 Point to remember
Use assertions rather than conditionals to fail automated tests.

✎ Your practice task

Change status to 404 and observe the branch.

Knowledge check

Which block runs when if is false and no else if matches?

🌐 Continue learning