CoursePart 1 — JavaScript for PlaywrightLesson 1.7
Lesson 1.7Part 1 — JavaScript for Playwright

Logical operators

&& || ?? and ?. — combining conditions and handling missing values.

Lesson 1.7of 24
Next

Step 1

The idea

01

Comparison operators answer one question at a time. Logical operators let you combine questions.

Imagine a bouncer at a club. The rule is: you must be over 18 AND have a valid ID. One condition isn't enough — both must be true.

Or a discount: you get a deal if you're a student OR a senior. Either one is enough.

JavaScript has three logical operators: && (AND), || (OR), and ! (NOT).

Step 2

See it run

02
Output
true
false
true
false
true
logical.js
const age = 22;
const hasId = true;
const isStudent = false;
const isSenior = true;

// AND — both must be true
console.log(age >= 18 && hasId);         // true  — both conditions met

// OR — at least one must be true
console.log(isStudent || isSenior);      // true  — senior is true

// AND — both must be true
console.log(isStudent && isSenior);      // false — student is false

// NOT — flips true to false and false to true
console.log(!hasId);                     // false — hasId is true, !true = false
console.log(!isStudent);                 // true  — isStudent is false, !false = true
Terminal
$ node logical.js

Step 3

Now you try

03

Write a check that answers: "Is this test eligible to run automatically?" — use these rules:

js
const testIsReady = true;
const environmentIsStaging = true;
const isBlocked = false;

// Write one console.log that uses && and ! to check:
// testIsReady AND environmentIsStaging AND NOT isBlocked

Expected output: true. Then set isBlocked = true and run again — should become false.

Step 4

Why a tester cares

04

Logical operators are the engine behind every access control system. Here's a simple role-based permission check in pure JavaScript:

permissions.js
const user = { name: "Priya", role: "editor", isActive: true };

// AND — all conditions must be true
const canPublish = user.isActive && user.role === "admin";

// OR — any condition is enough
const canEdit = user.role === "admin" || user.role === "editor";

// NOT — reverse the condition
const isGuest = !(user.role === "admin" || user.role === "editor");

console.log(`${user.name} can publish: ${canPublish}`);  // false
console.log(`${user.name} can edit: ${canEdit}`);        // true
console.log(`${user.name} is a guest: ${isGuest}`);      // false
Terminal
$ node permissions.js

This same pattern — combining boolean flags with &&, ||, ! — appears in login systems, dashboards, and form validation everywhere. Master these three operators and you can read almost any conditional logic.

Recap

3 key points
  • 1&& (AND) — both sides must be true. One false makes the whole thing false.
  • 2|| (OR) — at least one side must be true. One true is enough.
  • 3! (NOT) — flips the boolean: !true = false, !false = true.