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.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
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#
// ── 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'.
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#
- In JavaScript nothing stops a string price from entering the calculation.
- The bug is only found when someone notices a wrong total — maybe in production.
- In TypeScript, CartItem says price must be a number.
- 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#
It prevents type bugs, not logic bugs. You still need tests.
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?
Show solution
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#
When does JavaScript usually reveal a type mistake?
Want more? Practise all TypeScript MCQs · TypeScript interview questions
My notes#
Saved in this browser.