ProgramGuru

Comparison Operators

Learn Comparison Operators

Comparisons ask whether values are ordered relative to each other or equal, producing boolean results.

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 comparison operators.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Use >, <, >= and <= for numeric ordering, and === or !== for strict equality. JavaScript also permits lexicographic string comparison, which follows code-unit ordering rather than human-language sorting rules.

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 mark: number = 72;
 console.log(mark >= 40);
 console.log(mark < 90);
 console.log(mark === 72);

Verified output

true
true
true

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

72 passes the minimum mark of 40, is less than 90, and exactly equals 72. Each line prints a boolean.

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

Do not compare numeric-looking strings as if they were numbers: "20" < "3" can be true lexicographically.

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

Change mark to 40 and inspect the boundary behavior of >=. Replace >= with > and compare.

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

Check your understanding

Which operator includes equality in a lower-bound check?
Choose one answer. You can try again.

Answer and explanation

>=

Greater-than-or-equal-to succeeds at the boundary and above it.

Review the key idea

Comparison Operators: Use >, <, >= and <= for numeric ordering, and === or !== for strict equality.

Greater-than-or-equal-to succeeds at the boundary and above it.

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: Logical Operators →