Side by side
JavaScriptvsTypeScript
What is the difference between JavaScript and TypeScript?
Updated 2 min read7 differences
In short
JavaScript is the dynamically typed language that browsers and Node.js run, while TypeScript is a superset that adds static types and compiles to JavaScript.
JavaScript
JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
Read the page on JavaScriptTypeScript
TypeScript is a programming language built on JavaScript that adds static types, catching many bugs before the code runs, and compiles to plain JavaScript.
Read the page on TypeScriptJavaScript and TypeScript compared
| Aspect | JavaScript | TypeScript |
|---|---|---|
| Typing | Dynamic: types are checked only while the code runs | Static: types are checked by the compiler before the code runs |
| Runs in | Browsers and Node.js run it directly | Compiled or type-stripped to JavaScript before it runs |
| Type errors | Appear at runtime, during testing or in production | Appear in the editor and at build time |
| Setup | No build step; a <script> tag or node app.js is enough | Needs the tsc compiler, a bundler or a runtime that strips types |
| Editor tooling | Hints rely on inference and JSDoc comments | Rich autocomplete, safe renames and go-to-definition from types |
| Learning curve | Lower: fewer concepts to learn at first | Higher: adds types, generics, interfaces and compiler options |
| Best for | Small scripts, quick prototypes and learning the basics | Large codebases, teams and long-lived applications |
The difference, explained
JavaScript is the programming language of the web: every browser runs it, and runtimes like Node.js run it on servers. TypeScript is a superset of JavaScript, which means every valid JavaScript program is also valid TypeScript, plus optional type annotations such as name: string.
The essential difference is when type errors are found. JavaScript checks types only while the program runs, so passing a string where a number was expected may only show up as a bug in production. The TypeScript compiler checks types before the code runs, then removes them and emits plain JavaScript, so the types cost nothing at runtime.
The two are layers rather than rivals. TypeScript code always ends up as JavaScript, most popular libraries ship type definitions, and many projects migrate gradually by renaming files from .js to .ts one at a time. Recent Node.js versions and some other runtimes can even run .ts files directly by stripping out the type annotations.
A common misconception is that TypeScript makes code faster or checks data at runtime. Types are erased during compilation, so TypeScript cannot validate data arriving from an API or a form; you still need runtime checks for untrusted input.
Which one should you use?
Choose JavaScript when…
- You are writing a small script, prototype or one-off tool.
- You want zero build setup and to run code directly.
- You are still learning the fundamentals of the language.
Choose TypeScript when…
- The codebase is large or will be maintained for years.
- Several developers share the code and need clear contracts.
- You want the editor to catch mistakes before the code runs.
- You refactor often and want safe, tool-assisted changes.
The same function without and with types
// JavaScript: nothing stops a wrong call
function total(price, quantity) {
return price * quantity;
}
total(10); // runs and silently returns NaN// TypeScript: the mistake is caught before running
function total(price: number, quantity: number): number {
return price * quantity;
}
total(10); // Error: Expected 2 arguments, but got 1Readers ask
Is TypeScript better than JavaScript?
Neither is better in general. TypeScript pays off in larger or long-lived projects where catching errors early matters, while plain JavaScript is simpler for small scripts and quick experiments.
Do I need to learn JavaScript before TypeScript?
In practice, yes. TypeScript is JavaScript with types on top, so its syntax, runtime behavior and APIs are all JavaScript; knowing the base language first makes TypeScript much easier.
Does TypeScript run in the browser?
Not directly. Browsers only run JavaScript, so a build tool compiles or strips TypeScript into JavaScript before it reaches the browser.