ProgramGuru

Arithmetic Operators

Learn Arithmetic Operators

Arithmetic operators perform numeric addition, subtraction, multiplication, division, remainder and exponentiation.

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

Understand the concept

The main operators are +, -, *, /, % and **. Division can produce fractional numbers. The remainder operator % helps find divisibility, but with negative operands JavaScript remainder is not always a non-negative mathematical modulo.

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 a: number = 17;
 const b: number = 5;
 console.log(a + b);
 console.log(a / b);
 console.log(a % b);
 console.log(2 ** 3);

Verified output

22
3.4
2
8

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

17 + 5 is 22. Division produces 3.4, remainder produces 2 and 2 raised to power 3 is 8.

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 integer division: 17 / 5 is 3.4, not 3. Dividing by zero can produce Infinity.

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 the values to 20 and 4. Predict every result, then experiment with Math.floor(a / b).

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 returns the remainder?
Choose one answer. You can try again.

Answer and explanation

%

The percent symbol is JavaScript’s remainder operator.

Review the key idea

Arithmetic Operators: The main operators are +, -, *, /, % and **.

The percent symbol is JavaScript’s remainder operator.

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