ProgramGuru

TypeScript Operators

Learn TypeScript Operators

Operators combine or compare values, assign results, and control decisions. TypeScript checks whether the operand types make sense.

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

Understand the concept

The same runtime operators used in JavaScript also work in TypeScript: +, -, *, comparisons, logical operators and more. Some operators work differently based on operand types: + can concatenate strings, whereas subtraction expects values that can be treated as numbers. Prefer explicit conversions when handling user input.

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 quantity: number = 3;
 const cost: number = 20;
 const total = quantity * cost;
 console.log(total > 50);
 console.log(`Total: ${total}`);

Verified output

true
Total: 60

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

Multiplication yields 60. The greater-than comparison evaluates to true. The template literal then prints the same numeric total in a string.

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 assume + always means numeric addition. "4" + 2 produces "42" in JavaScript.

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

Replace * with + and predict the new total. Then compare a numeric operand with a string and observe the TypeScript diagnostic.

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 expression produces a boolean value?
Choose one answer. You can try again.

Answer and explanation

total > 50

Comparison operators evaluate conditions and produce true or false.

Review the key idea

TypeScript Operators: The same runtime operators used in JavaScript also work in TypeScript: +, -, *, comparisons, logical operators and more.

Comparison operators evaluate conditions and produce true or false.

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