Logical operators
&& || ?? and ?. — combining conditions and handling missing values.
Step 1
The idea
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
true
false
true
false
trueconst 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$ node logical.jsStep 3
Now you try
Write a check that answers: "Is this test eligible to run automatically?" — use these rules:
const testIsReady = true;
const environmentIsStaging = true;
const isBlocked = false;
// Write one console.log that uses && and ! to check:
// testIsReady AND environmentIsStaging AND NOT isBlockedExpected output: true. Then set isBlocked = true and run again — should become false.
Step 4
Why a tester cares
Logical operators are the engine behind every access control system. Here's a simple role-based permission check in pure JavaScript:
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$ node permissions.jsThis 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
- 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.