Playwright + JS · Lesson 1 of 35
Module 1: JavaScript Fundamentals
Try it yourself
Complete the verification function:
The editor has a worked example. Read it, tweak it, then press Mark as complete.
In this lesson
const vs let vs varIn modern test automation, we avoid var due to variable hoisting and scope leak issues:
const: For values that remain fixed during test runs (Base URLs, selectors, timeouts).let: For values that change dynamically (retry counts, loop indices, status flags).const baseUrl = "https://targetqa.com";
let retryAttempts = 0;
retryAttempts += 1;string: Test locators, text assertions ("Sign In").number: Element counts, status codes (200, 5000).boolean: Element states (true, false).null: Deliberate empty value.undefined: Unassigned variable.// Loop through test items with for...of:
const menuItems = ["Dashboard", "Orders", "Settings"];
for (const item of menuItems) {
console.log("Validating navigation item: " + item);
}
// Inspect configuration keys with for...in:
const config = { browser: "chromium", headless: true };
for (const key in config) {
console.log(key + ": " + config[key]);
}Hello and welcome to Playwright with JavaScript! In this first module, we explore JavaScript fundamentals essential for test automation. You will learn variable declarations, data types, comparison operators, and loops to control your automated test flows. Let's get started!
Part of the Playwright + JS course (Playwright with JavaScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.
=== checks both value and type without silent type coercion. == converts types unexpectedly (e.g., '0' == 0 is true), which causes test flakiness.
Use for...of to iterate through elements of arrays or lists of elements. Use for...in only when iterating through property keys of an object.
Finished reading?
Review the worked example in the editor, then mark the lesson complete.