ProgramGuru

Logical Operators

Learn Logical Operators

Logical operators combine conditions and provide short-circuit behavior.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind logical operators.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

&& evaluates the right operand only when the left is truthy; || evaluates it only when the left is falsy; ! negates truthiness. These operators can return operand values, not only boolean values, so an expression using strings or numbers should be read carefully.

See the concept visually

Use the diagram to locate the checks, values or branches involved in the example.

Logical Operators: illustrated flowCheck age >= 18; If true, check ticket; Combine with &&; Allow or deny1Check age >= 182If true, check ticket3Combine with &&4Allow or denyCompare the cases; they do not all necessarily execute.
Logical Operators: illustrated flow. Check age >= 18; If true, check ticket; Combine with &&; Allow or deny.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

const age: number = 20;
 const hasTicket: boolean = true;
 const mayEnter = age >= 18 && hasTicket;
 console.log(mayEnter);
 console.log(!mayEnter);

Verified output

true
false

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

age >= 18 evaluates to true, so JavaScript evaluates hasTicket. The conjunction is true and its negation is false.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

An expression such as value || 10 replaces legitimate 0 values. Use ?? when only null or undefined should trigger a fallback.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Set hasTicket to false and verify the output. Add a variable isMember and require all three conditions.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

When does the right side of A && B run?
Choose one answer. You can try again.

Answer and explanation

Only if A is truthy.

&& short-circuits when its first operand is falsy.

Review the key idea

Logical Operators: && evaluates the right operand only when the left is truthy; || evaluates it only when the left is falsy; ! negates truthiness.

&& short-circuits when its first operand is falsy.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Equality Operators →