Playwright + TS · Lesson 1 of 36

0/36 done

Module 1: JavaScript Fundamentals

1. JavaScript Fundamentals for Automation

12 minlesson

Your task

Complete the verification function:

  1. Declare const expectedTitle = "TargetQA - Learn Automation".
  2. Use an if/else block to check if actualTitle === expectedTitle.
  3. If true, set isMatch = true; otherwise set isMatch = false.

Write it in the editor, then press Check answer (Ctrl + Enter).

Variables: var vs let vs const

In modern JavaScript and TypeScript, we avoid var due to function-scoping and hoisting issues:

  • const: For values that never change (base URLs, test timeouts, page titles).
  • let: For values that get reassigned (retry counters, iteration indices).
javascript
const baseUrl = "https://targetqa.com";
let attemptCount = 0;
attemptCount++;

Data Types in JavaScript

  1. Primitives: string, number, boolean, null, undefined, symbol, bigint.
  2. null vs undefined:
    • undefined: A variable declared but not assigned a value.
    • null: An intentional assignment representing "no object/value".

Operators & Conditionals

  • Comparison: === (strict equality), !==, >, <, >=, <=.
  • Logical: && (AND), || (OR), ! (NOT).
  • Ternary: const status = isPassed ? "PASSED" : "FAILED";
javascript
if (responseStatus === 200) {
  console.log("Success");
} else if (responseStatus === 404) {
  console.log("Not Found");
} else {
  console.log("Unexpected status: " + responseStatus);
}

Loops for Automation

javascript
// Standard for loop:
for (let i = 0; i < 3; i++) {
  console.log("Retry attempt: " + i);
}

// for...of loop (iterates over values in array):
const tabs = ["Home", "Courses", "Practice"];
for (const tab of tabs) {
  console.log("Checking tab: " + tab);
}

// for...in loop (iterates over object keys):
const testConfig = { browser: "chromium", headless: true };
for (const key in testConfig) {
  console.log(key + ": " + testConfig[key]);
}

Key Takeaways & Walkthrough Summary

Hello and welcome! In this lesson, we are starting with JavaScript fundamentals, focusing specifically on what you need for automation testing. When writing test scripts, you will constantly manage variables, evaluate conditions, and loop through page elements. We will cover const and let, primitive data types, conditionals, and loops so you can write clean, resilient test scripts. Let's get started!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why should we always use === instead of == in JavaScript?

=== checks both value and type without type coercion. == converts types unexpectedly (for example, '0' == 0 is true, which causes silent automation bugs).

When should I use for...of vs for...in?

Use for...of to iterate through elements of arrays or lists of WebElements. Use for...in only when iterating through property names of an object.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

TypeScript
Skip