JavaScript vs TypeScript
The scripting language of the web versus its statically typed superset.
Overview
JavaScript is the language that browsers execute natively. TypeScript, announced by Microsoft in 2012, is a superset of JavaScript that adds a static type system and compiles back to plain JavaScript. Every valid JavaScript program is also a valid TypeScript program, which allows gradual adoption.
Because types are erased during compilation, TypeScript adds no runtime behaviour of its own. Its value lies in catching mistakes before the code runs and in giving editors accurate autocompletion and refactoring.
Side-by-side comparison
| JavaScript | TypeScript | |
|---|---|---|
| Typing | Dynamic, weak coercion rules | Static, structural, with inference |
| Runs in browsers | Directly | After compilation to JavaScript |
| Error detection | At run time | Many errors at compile time |
| Build step | Optional | Required (compile or strip types) |
| Governance | ECMAScript, TC39 committee | Developed by Microsoft as open source |
| First release | 1995 | 2012 |
Choosing between them
JavaScript suits small scripts, quick experiments and projects where a build step is unwelcome. TypeScript tends to pay off in larger codebases and teams, where type annotations act as checked documentation and make refactoring safer.
Choosing TypeScript does not mean leaving the JavaScript ecosystem: type definitions exist for most popular libraries, and the two languages can live side by side in one project.
Read more
- JavaScript: The programming language that powers interactive web pages and a large ecosystem of browser, server and desktop software.
- TypeScript: A language that extends JavaScript with a static type system and compiles to ordinary JavaScript.