TypeScript · Getting Started

JavaScript vs TypeScript

TypeScript is not a replacement for JavaScript — it is JavaScript with extra checks. Here is an honest comparison so you know when the extra checks are worth it.

Beginner 5 min Lesson 2 of 29Examples target TypeScript 5.x with "strict": true.
0/29 in TypeScript
On this page · 5 min · Beginner

Learning objectives#

  • Explain dynamic vs static typing
  • Compare tooling, safety and setup cost
  • Choose JavaScript or TypeScript for a project
  • Migrate a JavaScript file to TypeScript gradually

Explanation#

JavaScript is dynamically typed: a variable can hold a number now and a string later, and type mistakes only show up when that line runs.

TypeScript is statically typed: types are known while you write the code, so mistakes appear in the editor immediately.

TypeScript adds a build step (tsc, Vite, esbuild) and some learning. In return you get refactoring safety, better autocomplete and self-documenting code.

Small scripts and quick prototypes are fine in JavaScript. Team projects, long-lived apps and libraries benefit greatly from TypeScript.

You can migrate gradually: rename one .js file to .ts, fix its errors, and repeat. allowJs lets both live in one project.

Code example: The same function in both languages#

TypeScriptThe same function in both languages Illustrative
// ── JavaScript (calc.js) ─────────────────
function total(items) {
  return items.reduce((sum, i) => sum + i.price, 0);
}
total([{ price: '100' }, { price: 50 }]);   // "010050" — a string, silently wrong


// ── TypeScript (calc.ts) ─────────────────
interface CartItem {
  price: number;
}
function totalTs(items: CartItem[]): number {
  return items.reduce((sum, i) => sum + i.price, 0);
}
totalTs([{ price: '100' }, { price: 50 }]);
//         ~~~~~
// ✗ Type 'string' is not assignable to type 'number'.
Expected output
The JavaScript version runs and returns "010050" instead of 150, because the string price turns + into text joining. The TypeScript version refuses to compile until the price is a number.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. In JavaScript nothing stops a string price from entering the calculation.
  2. The bug is only found when someone notices a wrong total — maybe in production.
  3. In TypeScript, CartItem says price must be a number.
  4. The wrong call is flagged immediately in the editor.

Real-world use case#

A startup prototypes a landing page in JavaScript, then builds the real product dashboard in TypeScript because ten developers will work on it for years.

Common mistakes#

“TypeScript makes code bug-free”

It prevents type bugs, not logic bugs. You still need tests.

Converting a whole project at once

Hundreds of errors at once are overwhelming. Migrate file by file, starting with shared utilities.

Best practices#

  • Use TypeScript for anything that will be maintained
  • Migrate gradually with allowJs
  • Turn on strict mode early — it is harder to enable later
  • Keep using JavaScript for tiny scripts where setup is not worth it

Remember this#

JavaScript finds type bugs when the code runs. TypeScript finds them while you type.

Same runtime, extra safety, small setup cost.

Migrate one file at a time: .js → .ts.

Practice exercise#

Your task: List two situations where you would choose plain JavaScript and two where you would choose TypeScript.

Need a hint?
Think about project size, team size and how long the code will live.
Show solution
Text Illustrative
JavaScript: a one-off script to rename files; a quick CodePen experiment.
TypeScript: a team-built e-commerce app; a shared npm library used by other projects.

Mini quiz#

JavaScript vs TypeScript quizMultiple choice Question 1 of 3

When does JavaScript usually reveal a type mistake?

Score so far: 0/0

Want more? Practise all TypeScript MCQs · TypeScript interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.