ProgramGuru

TypeScript vs JavaScript

Learn TypeScript vs JavaScript

JavaScript is the language engines execute. TypeScript is designed to check JavaScript-style programs before execution.

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

Understand the concept

Valid JavaScript is generally valid TypeScript syntax, but the checker can report concerns about inferred types. A .ts file may include annotations such as : string, which a JavaScript engine normally cannot execute directly. Compilation strips type-only syntax; the runtime behavior comes from the resulting JavaScript.

See the concept visually

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

TypeScript vs JavaScript: illustrated flowTypeScript code; Type checking; JavaScript output; JavaScript engine1TypeScript code2Type checking3JavaScript output4JavaScript engineFollow the change from source values to the final result.
TypeScript vs JavaScript: illustrated flow. TypeScript code; Type checking; JavaScript output; JavaScript engine.

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

function double(value: number): number {
   return value * 2;
 }
 console.log(double(7));

Verified output

14

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

The parameter and return types are checked before emitting JavaScript. At runtime the function multiplies 7 by 2. In plain JavaScript the annotations would be absent.

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

TypeScript and JavaScript are not rival browser runtimes. Browsers run JavaScript, not TypeScript type checking.

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

Remove both annotations and see which types TypeScript infers. Then try calling double with a string and read the compiler 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 code does a browser normally execute?
Choose one answer. You can try again.

Answer and explanation

JavaScript emitted from TypeScript or written directly.

Browsers understand JavaScript. TypeScript can catch mistakes earlier but does not create a new browser language.

Review the key idea

TypeScript vs JavaScript: Valid JavaScript is generally valid TypeScript syntax, but the checker can report concerns about inferred types.

Browsers understand JavaScript. TypeScript can catch mistakes earlier but does not create a new browser language.

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: Features of TypeScript →